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 ...
随机推荐
- Android开发 ExpandableListView 可折叠列表详解
前言 在需要实现一个List的item需要包含列表的时候,我们就可以选择ExpandableListView. 其实这个View的原始设计还是ListView的那套.就是增加2层的ListView而已 ...
- html--div里让图片水平居中
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 异常处理记录: Servlet class X is not a javax.servlet.Servlet
使用Maven的tomcat插件启动Web项目, 访问资源时, 发生如下异常: https://stackoverflow.com/questions/1036702/my-class-is-not- ...
- iOS7新特性-完美解决iOS7关于自定义导航条UIBarButtonItem偏移的问题
前言: 本文由DevDiv社区@Vincent 原创,转载请注明出处! http://www.devdiv.com/iOS_iPhone-ios_ios_uibarbuttonitem_-thread ...
- HTML 排版标记
<p></p> : 表示一个段落 常用属性 : align : 水平对齐方式 取值 :left center right 和Word文档一样 : 段落有空行 <br ...
- keil mdk 无法添加对应容量的芯片
如果包已经安装好了 贴到 回到mdk,完事儿
- jsp页面判断当前请求的host
需要引入<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> ...
- java基础之final关键字
final: 意为终态.在java中得注意以下四点: 1.final是一个修饰符,可修饰变量,方法,类. 2.final修饰子类不可以被继承. 3.final修饰的方法不可以被重写(覆盖) 4.对于一 ...
- iOS 更新日志 - 持续更新中
本文只是为了简单记录一下每个正式版本发布时间和更新内容,只有这个初衷,从2019年9月25日开始,将会持续更新. iOS 13.1 - 2019年9月25日 iOS 13.1 iOS 13.1 包括错 ...
- zimage 和bzimage 有什么区别
在网络中,不少服务器采用的是Linux系统.为了进一步提高服务器的性能,可能需要根据特定的硬件及需求重新编译Linux内核.编译Linux 内核,需要根据规定的步骤进行,编译内核过程中涉及到几个重要的 ...