codeforces 545E E. Paths and Trees(单源最短路+总权重最小)
E. Paths and Treestime limit per test:3 secondsmemory limit per test:256 megabytesinput:standard inputoutput:standard outputLittle 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 V is the set of vertices, E is the set of edges). The shortest-path tree from vertex u is such graph G1 = (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 G1 are 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.
InputThe 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.
OutputIn 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.
Sample test(s)Input3 3
1 2 1
2 3 1
1 3 2
3Output2
1 2Input4 4
1 2 1
2 3 1
3 4 1
4 1 2
4Output4
2 3 4NoteIn 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.
题意:给n个点m条边,两个点间无重边,然后是m行的ui,vi,wi代表起始点和边的权重(第几个代表第几条边)然后在给起始点u,问选哪些边,可以满足从起始点u到达其它点距离最短,且使整张图的权重最小;
输出:所有边的总权重 以及选的边(递增顺序,若答案多组,输出任意一组)
思路:spfa,就是在更新每个点的最短路是,顺便记录和更新连到这个点的最短边;
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <climits>
#include <queue>
#define ll long long
#define INF 0x3f3f3f3f using namespace std;
const int MAXN = ;
ll total,head[MAXN],dis[MAXN],used[MAXN];
struct nodes
{
ll e,l,next,th;
} edge[MAXN];
struct nodes2
{
ll s,dis,p;
bool operator <(nodes2 q) const
{
return dis > q.dis;
}
} cur;
void add(ll x,ll y,ll l,ll th)
{
edge[total].e = y;
edge[total].l = l;
edge[total].th = th;
edge[total].next = head[x];
head[x] = total++;
}
void init( )
{
total = ;
memset(head,-,sizeof(head));
memset(used,-,sizeof(used));
fill(dis,dis+,INF);
} void spfa(int x,int n)
{
ll s,e,subdis,th;
priority_queue<nodes2>Q;
cur.dis = ,cur.s = x;
dis[x] = ;
Q.push(cur);
while(!Q.empty())
{
cur = Q.top();
Q.pop();
s = cur.s;
for(int i = head[s]; i != -; i = edge[i].next)
{
subdis = edge[i].l,e = edge[i].e,th = edge[i].th;
if(dis[e] > dis[s] + subdis)
{
dis[e] = dis[s] + subdis;
used[e] = th;
cur.s = edge[i].e,cur.dis = dis[e];
Q.push(cur);
}
}
}
ll d=;
for(int i = ; i <= n; i++)
d += dis[i];
printf("%lld\n",d);
sort(used,used+n);
for(int i = ; i< n; i++)
if(i != n)
printf("%lld ",used[i]);
else
printf("%lld\n",used[i]);
}
int main(void)
{
int n,m;
scanf("%d%d",&n,&m);
init();
for(int i = ; i <= m; i++)
{
ll x,y,l;
scanf("%lld %lld %lld",&x,&y,&l);
add(x,y,l,i);
add(y,x,l,i);
}
int s;
scanf("%d",&s);
spfa(s,n);
return ;
}
codeforces 545E E. Paths and Trees(单源最短路+总权重最小)的更多相关文章
- [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)
Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...
- 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)
关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...
- 用scheme语言实现SPFA算法(单源最短路)
最近自己陷入了很长时间的学习和思考之中,突然发现好久没有更新博文了,于是便想更新一篇. 这篇文章是我之前程序设计语言课作业中一段代码,用scheme语言实现单源最段路算法.当时的我,花了一整天时间,学 ...
- 单源最短路_SPFA_C++
当我们需要求一个点到其它所有点的最短路时,我们可以采用SPFA算法 代码特别好写,而且可以有环,但是不能有负权环,时间复杂度是O(α(n)n),n为边数,α(n)为n的反阿克曼函数,一般小于等于4 模 ...
- 【UVA1416】(LA4080) Warfare And Logistics (单源最短路)
题目: Sample Input4 6 10001 3 21 4 42 1 32 3 33 4 14 2 2Sample Output28 38 题意: 给出n个节点m条无向边的图,每条边权都为正.令 ...
- 【算法系列学习】Dijkstra单源最短路 [kuangbin带你飞]专题四 最短路练习 A - Til the Cows Come Home
https://vjudge.net/contest/66569#problem/A http://blog.csdn.net/wangjian8006/article/details/7871889 ...
- 模板C++ 03图论算法 1最短路之单源最短路(SPFA)
3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...
- 2018/1/28 每日一学 单源最短路的SPFA算法以及其他三大最短路算法比较总结
刚刚AC的pj普及组第四题就是一种单源最短路. 我们知道当一个图存在负权边时像Dijkstra等算法便无法实现: 而Bellman-Ford算法的复杂度又过高O(V*E),SPFA算法便派上用场了. ...
- PAT All Roads Lead to Rome 单源最短路
思路:单源最短路末班就好了,字符串映射成数字处理. AC代码 //#define LOCAL #include <stdio.h> #include <string.h> #i ...
随机推荐
- Qt下QMainWindow内部QTableView不能自适应大小
中央窗体设置的是一个QWidget 一直排查不到原因 最后发现为 因为布局中为QTableView设置了对齐方式 取消即可!
- jQuery.event.move
http://stephband.info/jquery.event.move/ Move events movestart Fired following touchmove or mousemov ...
- PostgreSQL:COALESCE函数
COALESCE函数是返回参数中的第一个非null的值,它要求参数中至少有一个是非null的,如果参数都是null会报错. select COALESCE(null,null); //报错 selec ...
- STL vector容器需要警惕的一些坑
从迭代器中取值切记需要判断是否为空 例如: vector<int> vtTest; vtTest.clear(); if (vtTest.empty()){ ; } ]; 如果没有忘了判断 ...
- Joomla - 部署(线上部署)
一.线上部署 线上部署可以理解为把本地网站迁移到线上,使用 akeeba backup 进行备份和迁移即可 参考 Joomla - akeeba backup(joomla网站备份.迁移扩展)的第三. ...
- videojs实现双击视频全屏播放、播放器全屏时视频未全屏
https://blog.csdn.net/staritstarit/article/details/78451963 暂停时只能使用左下角的暂停按钮,点击视频时不再响应 使用width和height ...
- 密码学笔记(5)——Rabin密码体制和语义安全性
一.Rabin密码体制 Rabin密码体制是RSA密码体制的一种,假定模数$n=pq$不能被分解,该类体制对于选择明文攻击是计算安全的.因此,Rabin密码体制提供了一个可证明安全的密码体制的例子:假 ...
- C#窗体阴影
/// <summary> /// 边框阴影 /// </summary> protected override CreateParams CreateParams { get ...
- Lint found fatal errors while assembling a release target问题的解决方案
此问题发生在编译为 release 版本时,出现错误提示如下: Lint found fatal errors while assembling a release target. To procee ...
- Windows 系统文件夹目录挂载到 Linux服务器中
在Windows系统文件上传到Linux服务器时有时候很麻烦,因为Linux无界面的系统不像Windows系统一样,可以直接复制粘贴,下面方法可以解决Windows系统文件拷贝到Linux服务器. 1 ...