问题描述
G国国王来中国参观后,被中国的高速铁路深深的震撼,决定为自己的国家也建设一个高速铁路系统。
建设高速铁路投入非常大,为了节约建设成本,G国国王决定不新建铁路,而是将已有的铁路改造成高速铁路。现在,请你为G国国王提供一个方案,将现有的一部分铁路改造成高速铁路,使得任何两个城市间都可以通过高速铁路到达,而且从所有城市乘坐高速铁路到首都的最短路程和原来一样长。请你告诉G国国王在这些条件下最少要改造多长的铁路。
输入格式
输入的第一行包含两个整数n, m,分别表示G国城市的数量和城市间铁路的数量。所有的城市由1到n编号,首都为1号。
接下来m行,每行三个整数a, b, c,表示城市a和城市b之间有一条长度为c的双向铁路。这条铁路不会经过a和b以外的城市。
输出格式
输出一行,表示在满足条件的情况下最少要改造的铁路长度。
样例输入
4 5
1 2 4
1 3 5
2 3 2
2 4 3
3 4 2
样例输出
11
评测用例规模与约定
对于20%的评测用例,1 ≤ n ≤ 10,1 ≤ m ≤ 50;
对于50%的评测用例,1 ≤ n ≤ 100,1 ≤ m ≤ 5000;
对于80%的评测用例,1 ≤ n ≤ 1000,1 ≤ m ≤ 50000;
对于100%的评测用例,1 ≤ n ≤ 10000,1 ≤ m ≤ 100000,1 ≤ a, b ≤ n,1 ≤ c ≤ 1000。输入保证每个城市都可以通过铁路达到首都。
/*数据量过大,需采用堆优化的dijkstra算法*/
#include <bits/stdc++.h>
using namespace std;
const int Inf=0x7fffffff;
int n,m;
struct edge
{
int v;
int cost;
edge(int x,int c):v(x),cost(c){}
};
struct cmp
{
bool operator()(edge &a,edge &b)
{
return a.cost>b.cost;
}
};
vector<edge> vg[10000];
int main()
{
scanf("%d %d",&n,&m);
for(int i=0;i<m;++i)
{
int x,y,w;
scanf("%d %d %d",&x,&y,&w);
--x,--y;
vg[x].push_back(edge(y,w));
vg[y].push_back(edge(x,w));
}
//dijkstra
priority_queue<edge,vector<edge>,cmp> pq;
vector<int> dist(n,Inf);
vector<int> cost(n,Inf);
vector<bool> visit(n,0);
pq.push(edge(0,0));
dist[0]=cost[0]=0;
while(!pq.empty())
{
edge t=pq.top();//最短路径(花费)边
pq.pop();
visit[t.v]=1;//访问该节点
for(size_t i=0;i<vg[t.v].size();++i)
{
edge e=vg[t.v][i];//该节点的所有邻接边
if(visit[e.v])continue;//另外一端节点访问过了,跳过
if(dist[e.v]>dist[t.v]+e.cost)//更新最短路径
{
dist[e.v]=dist[t.v]+e.cost;
cost[e.v]=e.cost;
pq.push(edge(e.v,dist[e.v]));//新的更新边
}
else if(dist[e.v]==dist[t.v]+e.cost)
{
cost[e.v]=min(cost[e.v],e.cost);
}
}
}
int sum=0;
for(int i=0;i<n;++i)
sum+=cost[i];
printf("%d\n",sum);
return 0;
}