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 ...
随机推荐
- UVA - 374
https://vjudge.net/problem/19685/origin 费马小定理优化快速幂 因为加了费马小定理优化,小心2 2 2这种情况,会出现0 0 2,也就是0的0次方,实际答案为0 ...
- 深入理解Java虚拟机(类加载机制)
文章首发于微信公众号:BaronTalk 上一篇文章我们介绍了「类文件结构」,这一篇我们来看看虚拟机是如何加载类的. 我们的源代码经过编译器编译成字节码之后,最终都需要加载到虚拟机之后才能运行.虚拟机 ...
- 工作中遇到的bug
1. Error: No PostCSS Config found in.. 在项目根目录新建postcss.config.js文件,并对postcss进行配置: module.exports = { ...
- springMVC项目创建及导入包项
springMVC项目创建及导入包项 - zhangzhetaojj的博客 - CSDN博客https://blog.csdn.net/zhangzhetaojj/article/details/50 ...
- springboot与任务(邮件任务)
邮件发送需要引入spring-boot-starter-mail Spring Boot 自动配置MailSenderAutoConfiguration 定义MailProperties内容,配置在a ...
- EF(Entity Framwork)结构
初次接触EF,看了一些资料,将自己对EF结构的理解记录如下: EF的核心是EDM----实体数据模型(.edmx).它由三部分组成:概念模型(.csdl文件).存储模型(.ssdl文件).映射规范(. ...
- windows 可执行文件分析
windows可执行文件是什么? 是具有PE文件格特性的文件,例如:.exe.dll.ocx等文件. 注:(这里只是让大家能明了一些,其实,可执行与否,和后缀没有什么关系,后缀只是windows方便管 ...
- Spring cloud config client获取不到配置中心的配置
Spring cloud client在配置的时候,配置文件要用 bootstrap.properties 贴几个说明的链接.但是觉得说的依然不够详细,得空详查. 链接1 链接2 链接3 原文地址:h ...
- neo4j 实战、实例、示例 创建电影关系图 -1
1. 创建关系 因为代码占篇幅太大,创建整个"电源关系图"的代码在文章最下方. 2. 简单分析创建语句 2.1 创建电影节点 CREATE (TheMatrix:Movie {ti ...
- Chapter 6 排序
Chapter 6 排序 1- 直接插入排序 O(n2) O(1) 2- 折半插入排序 O(n2) O(1) 适合关键字较多 3- 希尔排序O(nlogn) O(1) 又名,缩小增量排序 ...