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
inputstandard input
outputstandard 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 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.
输入
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.
样例
input
3 3
1 2 1
2 3 1
1 3 2
3
output
2
1 2
Note
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.
题意
给你一个无向图,求从u点出发的到所有点的最短路树,且让最短路树的边权和最小。
题解
最短路+贪心
每次松弛如果发现有多个前驱符合最短路,选边权最小的前驱。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
typedef __int64 LL;
struct Edge {
int u, v, w, id,tag;
Edge(int u, int v, int w,int id) :u(u), v(v), w(w),id(id), tag(0) {}
};
const int maxn = 3e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
int n, m;
vector<int> G[maxn];
vector<Edge> egs;
void addEdge(int u, int v, int w, int id) {
egs.push_back(Edge(u, v, w, id));
G[u].push_back(egs.size() - 1);
}
int inq[maxn],pre[maxn];
LL d[maxn];
void spfa(int s) {
for (int i = 0; i <= n; i++) d[i] = INF;
memset(inq, 0, sizeof(inq));
memset(pre, -1, sizeof(pre));
queue<int> Q;
d[s] = 0, inq[s] = 1, Q.push(s);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = 0;
for (int i = 0; i < G[u].size(); i++) {
Edge& e = egs[G[u][i]];
if (d[e.v] > d[u] + e.w) {
d[e.v] = d[u] + e.w;
pre[e.v] = G[u][i];
if (!inq[e.v]) inq[e.v] = 1, Q.push(e.v);
}
else if (d[e.v] == d[u] + e.w) {
Edge& ee = egs[pre[e.v]];
if (ee.w > e.w) pre[e.v] = G[u][i];
}
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m;i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w),u--,v--;
addEdge(u, v, w, i);
addEdge(v, u, w, i);
}
int s;
scanf("%d", &s),s--;
spfa(s);
vector<int> ans;
LL cnt = 0;
for (int i = 0; i < n; i++) {
if (pre[i] != -1) {
ans.push_back(egs[pre[i]].id);
cnt += egs[pre[i]].w;
}
}
printf("%I64d\n", cnt);
if (ans.size() == 0) return 0;
for (int i = 0; i < ans.size() - 1; i++) printf("%d ", ans[i]);
printf("%d\n", ans[ans.size() - 1]);
return 0;
}
Codeforces Round #303 (Div. 2) E. Paths and Trees 最短路+贪心的更多相关文章
- 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) E. Paths and Trees Dijkstra堆优化+贪心(!!!)
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 ...
随机推荐
- 未能找到任何适合于指定的区域性或非特定区域性的资源。请确保在编译时已将“xxx.Resources.resources”正确嵌入或链接到程序集
今天在测试一个工程的时候,突然遇到了这样一个问题: 错误信息:System.Resources.MissingManifestResourceException: 未能找到任何适合于指定的区域或非特定 ...
- oracle生成.net的guid方法;
最近在做一个T1的.NET项目,数据库oracle的时候,遇到一个问题..NET里面的某个数据库表类的某个字段是guid类型.但是用oracle生成的guid.跟.NET的guid 无法识别.导致报错 ...
- 友盟分享--集成QQ和微信
随着社交工具的应用范围越来越广,分享一些内容的功能也开始要求实现了. 用得比较多的第三方,比如说友盟,比如说Share等等... 前几天刚用友盟写了集成QQ和微信客户端的功能,觉得有必要分享一下. 在 ...
- MPlayerX For Mac白屏问题
在Mac App store下载了MPlayerX后,如果系统版本是10.10的,用MPlayerX看视屏当选择全屏后会出现白屏现象只有声音退出全屏后仍旧是白屏. 这是因为MPlayerX已经在Mac ...
- OC9_代理正向传值
// // ProtectedDeleagate.h // OC9_代理正向传值 // // Created by zhangxueming on 15/6/24. // Copyright (c) ...
- vi 命令行模式功能键
目录 目录内容 I 切换到插入模式,此时光标位于开始输入文件处 A 切换到插入模式,并从目前光标所在位置的下一个位置开始输入文字 O 切换到插入模式,并从行首开始插入行的一行 [ctrl]+[b] 屏 ...
- JSLint notepad++使用
1.JSLint简介 JSLint定义了一组编码约定,这比ECMA定义的语言更为严格.这些编码约定汲取了多年来的丰富编码经验,并以一条年代久远的编程原则 作为宗旨:能做并不意味着应该做.JSLint会 ...
- OC 内存管理机制总结
OC 内存管理机制总结 一:OC内存管理机制目前分为两块,其一自动内存管理机制,其二手动内存管理机制: 1.首先我们从自动内存管理机制讲起: 1)什么是自动内存管理机制,自动内存管理机制就是程序中所创 ...
- 有趣的checkbox动画切换状态(支付宝转账成功显示)--第三方开源--AnimCheckBox
这个很有趣的指标通过AnimCheckBox实现,下载地址:https://github.com/lguipeng/AnimCheckBox 代码: activity_main.xml: <Re ...
- Laravel 5 基础(三)- 向视图传送数据
我们在Routes.php中新建一个路由 Route::get('about', 'PagesController@about'); 在浏览器中浏览会获得一个错误,错误信息仅仅是一个提示信息,缺少细节 ...