试题名称: | 交通规划 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: |
问题描述
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。输入保证每个城市都可以通过铁路达到首都。
|
/************************************************************************* > File Name: 交通规划.cpp > Author: ZhangHaoRan > Mail: chilumanxi@gmail.com > Created Time: 2016年10月18日 星期一 17时55分26秒 ************************************************************************/ #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<vector> #include<set> #include<map> #include<queue> #include<list> #include<algorithm> #include<cmath> using namespace std; int n, m; const int MAXN = 10010; const int INF = 0x3f3f3f3f; struct qnode{ int v; int c; qnode(int _v = 0, int _c = 0):v(_v), c(_c){} bool operator <(const qnode &r)const{ return c > r.c; } }; struct Edge{ int v, cost; Edge(int _v = 0, int _cost = 0):v(_v), cost(_cost){} }; vector<Edge>E[MAXN]; bool vis[MAXN]; int dist[MAXN]; void Dijkstra(int n, int start){ memset(vis, false, sizeof(vis)); for(int i = 1; i <= n; i ++) dist[i] = INF; priority_queue<qnode> que; while(!que.empty()) que.pop(); dist[start] = 0; que.push(qnode(start, 0)); qnode tmp; while(!que.empty()){ tmp = que.top(); que.pop(); int u = tmp.v; if(vis[u]) continue; vis[u] = true; for(int i = 0; i < E[u].size(); i ++){ int v = E[tmp.v][i].v; int cost = E[u][i].cost; if(!vis[v] && dist[v] > dist[u] + cost){ dist[v] = dist[u] + cost; que.push(qnode(v, dist[v])); } } } } queue<int> q; int minn[MAXN]; void bfs(){ q.push(1); while(!q.empty()){ int cur = q.front(); q.pop(); for(int i = 0; i < E[cur].size(); i ++){ if(dist[E[cur][i].v] == dist[cur] + E[cur][i].cost){ q.push(E[cur][i].v); minn[E[cur][i].v] = min(minn[E[cur][i].v], E[cur][i].cost); } } } int ans = 0; for(int i = 2; i <= n; i ++){ ans += minn[i]; } printf("%d\n", ans); } void addedge(int u, int v, int w){ E[u].push_back(Edge(v, w)); } int main(void){ scanf("%d%d", &n, &m); int x, y, v; memset(minn, INF, sizeof(minn)); for(int i = 0; i < m; i ++){ scanf("%d%d%d", &x, &y, &v); addedge(x, y, v); addedge(y, x, v); } Dijkstra(n, 1); bfs(); return 0; }
查看原文:http://chilumanxi.org/2016/10/18/ccf-201612-4-%e4%ba%a4%e9%80%9a%e8%a7%84%e5%88%92/