Codeforces Round #303 (Div. 2) E. Paths and Trees Dijkstra堆优化+贪心(!!!)
3 seconds
256 megabytes
standard input
standard output
Little girl Susie accidentally found her elder brother's notebook. She has many things to do, more important than solving problems, but she found this problem too interesting, so she wanted to know its solution and decided to ask you about it. So, the problem statement is as follows.
Let's assume that we are given a connected weighted undirected graph G = (V, E) (here Vis the set of vertices, E is the set of edges). The shortest-path tree from vertex u is such graphG1 = (V, E1) that is a tree with the set of edges E1 that is the subset of the set of edges of the initial graph E, and the lengths of the shortest paths from u to any vertex to G and to G1are the same.
You are given a connected weighted undirected graph G and vertex u. Your task is to find the shortest-path tree of the given graph from vertex u, the total weight of whose edges is minimum possible.
The first line contains two numbers, n and m (1 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of vertices and edges of the graph, respectively.
Next m lines contain three integers each, representing an edge — ui, vi, wi — the numbers of vertices connected by an edge and the weight of the edge (ui ≠ vi, 1 ≤ wi ≤ 109). It is guaranteed that graph is connected and that there is no more than one edge between any pair of vertices.
The last line of the input contains integer u (1 ≤ u ≤ n) — the number of the start vertex.
In the first line print the minimum total weight of the edges of the tree.
In the next line print the indices of the edges that are included in the tree, separated by spaces. The edges are numbered starting from 1 in the order they follow in the input. You may print the numbers of the edges in any order.
If there are multiple answers, print any of them.
3 3
1 2 1
2 3 1
1 3 2
3
2
1 2
4 4
1 2 1
2 3 1
3 4 1
4 1 2
4
4
2 3 4
In the first sample there are two possible shortest path trees:
- with edges 1 – 3 and 2 – 3 (the total weight is 3);
- with edges 1 – 2 and 2 – 3 (the total weight is 2);
And, for example, a tree with edges 1 – 2 and 1 – 3 won't be a shortest path tree for vertex 3, because the distance from vertex 3 to vertex 2 in this tree equals 3, and in the original graph it is 1.
题目:一个带权无向图,求一个新图G’=(V,E’),使得源点s到新图各个点的最短距离等于在原图中的最短距离,输出边权值最小的新图;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=300000; int used[maxn+10],cost[maxn+10],flag[maxn+10],vis[maxn+10];
ll dis[maxn+10]; struct Edge{
int to,w,id;
};
vector<Edge> G[maxn+10]; struct node{
int to;
ll dis;
bool operator <(const node a) const{
return this->dis>a.dis;
}
};
int main()
{
int n,m,u,v,w,s;
while(~scanf("%d %d",&n,&m))
{
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&u,&v,&w);
G[u].push_back((Edge){v,w,i});
G[v].push_back((Edge){u,w,i});
} scanf("%d",&s);
for(int i=1;i<=n;i++)
{
dis[i]=1e16;
cost[i]=1e9+10;
flag[i]=0;
used[i]=0;
}
priority_queue<node> q;
q.push((node){s,0});
dis[s]=0;
while(q.size())
{
node cur=q.top();q.pop();
if(dis[cur.to]<cur.dis) continue;
int u=cur.to;
used[u]=1;
for(int i=0;i<G[u].size();i++)
{
Edge e=G[u][i];
if(used[e.to]) continue;
if(dis[e.to]>dis[u]+e.w)
{
dis[e.to]=dis[u]+e.w;
flag[e.to]=e.id;
cost[e.to]=e.w;
q.push((node){e.to,dis[e.to]});//q要放在更新函数内,否则会爆优先队列
}
else if(dis[e.to]==dis[u]+e.w&&cost[e.to]>e.w)
{
cost[e.to]=e.w;
flag[e.to]=e.id;
q.push((node){e.to,dis[e.to]});
}
}
} ll ans=0;
for(int i=1;i<=n;i++)
if(i!=s) ans+=cost[i];
printf("%lld\n",ans);
for(int i=1;i<=n;i++)
if(i!=s) printf("%d ",flag[i]);
printf("\n");
}
return 0;
}
分析:神奇的一道题目,
1.需要使用堆优化的Dijkstra求一遍最短路;
2.贪心:在求最短路的过程中,假如起点到其余各点只有一条最短路的话,那么显然就是这些最短路
组成的图,但是假如到达同一个点有多条最短路的话,那么就要进行贪心,比如样例中的2和3号节点都可以
最短路到达1,但是因为2与1直接相连的那条边权值要小,所以就选走2这条路的。
3.其实求最后的起点到每个节点的最短路,就是一棵树
Codeforces Round #303 (Div. 2) E. Paths and Trees Dijkstra堆优化+贪心(!!!)的更多相关文章
- Codeforces Round #303 (Div. 2) E. Paths and Trees 最短路+贪心
题目链接: 题目 E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes inputs ...
- Codeforces Round #303 (Div. 2)E. Paths and Trees 最短路
E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- 水题 Codeforces Round #303 (Div. 2) D. Queue
题目传送门 /* 比C还水... */ #include <cstdio> #include <algorithm> #include <cstring> #inc ...
- DP Codeforces Round #303 (Div. 2) C. Woodcutters
题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...
- 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
- 水题 Codeforces Round #303 (Div. 2) A. Toy Cars
题目传送门 /* 题意:5种情况对应对应第i或j辆车翻了没 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 3: if both cars turned over during th ...
- Codeforces Round #303 (Div. 2)
A.Toy Cars 题意:给出n辆玩具车两两碰撞的结果,找出没有翻车过的玩具车. 思路:简单题.遍历即可. #include<iostream> #include<cstdio&g ...
- Codeforces Round #303 (Div. 2)(CF545) E Paths and Trees(最短路+贪心)
题意 求一个生成树,使得任意点到源点的最短路等于原图中的最短路.再让这个生成树边权和最小. http://codeforces.com/contest/545/problem/E 思路 先Dijkst ...
- Codeforces Round #303 (Div. 2) D. Queue 傻逼题
C. Woodcutters Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...
随机推荐
- iview Upload组件多个文件上传
使用 iview Upload 上传组件 手动上传 包括单个文件和多个文件 思路:创建一个数组 把需要上传的文件 push到这个数组里面 1.引用组件 2.手动上传,根据官方文档 设置:before ...
- SheetJS js-xlsx 的使用, exceljs
js-xlsx 官方文档:https://sheetjs.gitbooks.io/docs/#sheetjs-js-xlsx npm xlsx地址:https://www.npmjs.com/pack ...
- 2019JAVA第十一次实验报告
#Java实验报告 班级 计科二班 学号 20188442 姓名 吴怡君 完成时间 2019.11.22 评分等级 简易记事本 实验代码 package Domon10; import java.aw ...
- Solr 4.4.0增加core
假设现在我们现在需要增加一个新的core名称为"core1" 1. 在solr_home目录新建目录core1 $ mkdir /usr/local/contentplatform ...
- Java 读取Json文件内容
读取json文件为String类型: import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logge ...
- ftp读取图片并转Base64
public String download(String ftpUrl,String sfzh){ FTPClient ftpClient = new FTPClient(); InputStrea ...
- 通过sohu获取浏览器端IP地址
接口:http://pv.sohu.com/cityjson?ie=utf-8
- npm学习(十)之如何使用创建、发布、使用作用域包
前言 要求npm版本2或更高 作用域用于将相关包分组在一起,并为npm模块创建一个名称空间(类似于域).这里有更详细的解释. 如果一个包的名称以@开头,那么它就是一个有作用域的包.范围是@和斜杠之间的 ...
- MySQL数据库入门常用基础命令
MySQL数据库入门———常用基础命令 数据——公司的生命线,因此在大多数的互联网公司,都在使用开源的数据库产品,MySQL也因此关注度与使用率非常的高,所以做为运维的屌丝们,掌握它的一些基 ...
- PDF转图片,在线PDF转JPG/PNG
[在线DEMO](https://oktools.net/pdf2img) 原理 使用pdf.js预览图片,pdf.js将pdf通过canvas将每一页渲染出来,然后我们通过canvas的toData ...