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.
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.
3 3
1 2 1
2 3 1
1 3 2
3
2
1 2
4 4
1 2 1
2 3 1
3 4 1
4 1 2
4
4
2 3 4
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.其实求最后的起点到每个节点的最短路,就是一棵树