CCF 201609-4 交通规划(BFS)
来源:互联网 发布:29岁了转行做程序员 编辑:程序博客网 时间:2024/05/20 18:51
试题编号: 201609-4
试题名称: 交通规划
时间限制: 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。输入保证每个城市都可以通过铁路达到首都。
代码:
#include<bits/stdc++.h>using namespace std;const long long INF = 0x3f3f3f3f3f3f3f3f;queue<int> q;struct edge{ int to;//到达的点 int len;//长度 int flag = 0; edge(int to,int len):to(to),len(len){}};vector<edge> e[100007];int vis[10007],total = 0;int dis[100007],cost[10007];void dfs(int temp){ int begin = temp; vis[begin] = 1; q.push(begin); while (!q.empty()) { begin = q.front(); vis[begin] = 0; q.pop(); for (int i = 0; i < e[begin].size(); i++) { if (dis[begin] + e[begin][i].len < dis[e[begin][i].to]) { dis[e[begin][i].to] = dis[begin] + e[begin][i].len; cost[e[begin][i].to] = e[begin][i].len;//到每个点的最小花费等于新加入边的长度 q.push(e[begin][i].to); vis[e[begin][i].to] = 1; } if (dis[e[begin][i].to] == dis[begin] + e[begin][i].len && cost[e[begin][i].to] > e[begin][i].len)//关键点:如果到某个点距离相同的话,取短的那条边 { cost[e[begin][i].to] = e[begin][i].len; } } }}int main(){ int n, m,x,y,len;//点边的个数 //构建图 cin >> n >> m; memset(vis, 0, sizeof(vis)); memset(dis, INF, sizeof(dis)); memset(cost, INF, sizeof(cost)); //构建图 for (int i = 0; i < m; i++) { cin >> x >> y >> len; e[x-1].push_back(edge(y-1, len)); e[y-1].push_back(edge(x-1, len)); } //初始化处理 while (!q.empty()) { q.pop(); } dis[0] = 0; cost[0] = 0; //初始化处理 dfs(0); for (int i = 0; i < n; i++) { total += cost[i]; } cout << total; return 0;}
- CCF 201609-4 交通规划(BFS)
- CCF 201609-4 交通规划
- ccf 201609-4交通规划
- CCF 交通规划 201609-4(dijkstra)
- CCF 201612-4 交通规划
- CCF 201612-4 交通规划
- CCF考试——201609-4交通规划
- CCF 201612-4 交通规划(spfa)
- ccf-2016-09-4-交通规划
- CCF-交通规划
- CCF交通规划
- CCF 交通规划
- ccf 交通规划
- ccf交通规划
- CCF交通规划
- CCF 交通规划
- ccf 交通规划
- CCF-20160904-交通规划
- 对Linux中权限的理解与重要命令
- 第5章 树莓派GPIO控制
- solr基本配置
- 搁浅的冬的博客
- 开发android项目的常用jar包
- CCF 201609-4 交通规划(BFS)
- Leha and Function CodeForces
- JavaWeb框架-MyBatis-2-这个世界的增删改查~
- corejava
- 尺取法 poj 3061
- FJUT 3104 海平面上升 最短路 或者 离线+并查集
- Android安全二
- LWC 59:731. My Calendar II
- 二级列表实现购物车功能