E. Paths and Trees
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
input
3 3
1 2 1
2 3 1
1 3 2
3
output
2
1 2
input
4 4
1 2 1
2 3 1
3 4 1
4 1 2
4
output
4
2 3 4
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.

题目:一个带权无向图,求一个新图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堆优化+贪心(!!!)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 水题 Codeforces Round #303 (Div. 2) D. Queue

    题目传送门 /* 比C还水... */ #include <cstdio> #include <algorithm> #include <cstring> #inc ...

  4. DP Codeforces Round #303 (Div. 2) C. Woodcutters

    题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...

  5. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

  6. 水题 Codeforces Round #303 (Div. 2) A. Toy Cars

    题目传送门 /* 题意:5种情况对应对应第i或j辆车翻了没 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 3: if both cars turned over during th ...

  7. Codeforces Round #303 (Div. 2)

    A.Toy Cars 题意:给出n辆玩具车两两碰撞的结果,找出没有翻车过的玩具车. 思路:简单题.遍历即可. #include<iostream> #include<cstdio&g ...

  8. Codeforces Round #303 (Div. 2)(CF545) E Paths and Trees(最短路+贪心)

    题意 求一个生成树,使得任意点到源点的最短路等于原图中的最短路.再让这个生成树边权和最小. http://codeforces.com/contest/545/problem/E 思路 先Dijkst ...

  9. Codeforces Round #303 (Div. 2) D. Queue 傻逼题

    C. Woodcutters Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...

随机推荐

  1. [Vuejs] 点击单选框触发两次点击事件的处理

    <el-radio-group v-model="uploadStatus" class="upload-status-radio"> <el ...

  2. lua基础学习(五)

    一.Lua 模块与包 模块类似于一个封装库,从 Lua 5.1 开始,Lua 加入了标准的模块管理机制,可以把一些公用的代码放在一个文件里,以 API 接口的形式在其他地方调用,有利于代码的重用和降低 ...

  3. javascript 数据类型 undefined 和null

    数据类型 undefind null boolean number string object type of 功能:检测变量类型 语法:type of 变量或 type of (变量) consol ...

  4. python 并发编程 协程 目录

    python 并发编程 协程 协程介绍 python 并发编程 协程 greenlet模块 python 并发编程 协程 gevent模块 python 并发编程 基于gevent模块实现并发的套接字 ...

  5. Elasticsearch-集群增加节点

    ES-在集群中加入节点 查看分片信息 FengZhendeMacBook-Pro:nacos FengZhen$ curl 'localhost:9200/_cat/shards?v' index s ...

  6. 几张图让你看懂WebAssembly

    几张图让你看懂WebAssembly:https://www.jianshu.com/p/bff8aa23fe4d

  7. Virtual DOM和snabbdom.js

    Virtual DOM和snabbdom.js:https://www.jianshu.com/p/1f1ef915e83e

  8. hdu3829 二分匹配 最大独立集

    Cat VS Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Problem ...

  9. C++中多态的概念和意义

    1,函数重写回顾: 1,父类中被重写的函数依然会继承给子类: 2,子类中重写的函数将覆盖父类中的函数: 1,重写父类当中提供的函数是因为父类当中提供的这个函数版本不能满足我们的需求,因此我们要重写: ...

  10. python-day11(正式学习)

    目录 文件高级应用 多重操作 r+t:可读,可写(文件名为a) w+t:可写可读 a+t:可追加可读 文件内指针移动及一些操作 指针移动seek(offset,whence) 寻找指针位置tell() ...