B - 畅通工程续

木槿槿槿 2019-08-19 08:29:44
某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。 现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。 Input 本题目包含多组数据,请处理到文件结束。 每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。 接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。 再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。 Output 对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1. Sample Input 3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2 Sample Output 2 -1 这道题用floyd模板就行。但是要注意每两个城镇之间不一定只有一条路,所以要比较一下两城镇间路的大小再存入。还有一些城镇之间可能没有联通,如果不能到达终点,存放的数值应该是最大值。这两点都注意就应该没什么问题了。 代码如下: #include"stdio.h" #include"string.h" #include"math.h" #include"stdlib.h" #include"algorithm" using namespace std; #define inf 0x3f3f3f3f int main() { int m,n; while(~scanf("%d %d",&m,&n)) { int map[105][105]; for(int i=0;i<m;i++) { for(int j=0;j<m;j++) { if(i!=j) { map[i][j]=inf; } } map[i][i]=0; } for(int i=0;i<n;i++) { int a,b,c; scanf("%d %d %d",&a,&b,&c); if(c<map[a][b]) { map[a][b]=c; map[b][a]=c; } } int s,t;scanf("%d %d",&s,&t); for(int k=0;k<m;k++) { for(int i=0;i<m;i++) { for(int j=0;j<m;j++) { if(map[i][k]+map[k][j]<map[i][j]) { map[i][j]=map[i][k]+map[k][j]; } } } } if(map[s][t]==inf) { printf("-1\n"); } else { printf("%d\n",map[s][t]); } } return 0; }
...全文
43 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Italink 2019-08-20
  • 打赏
  • 举报
回复
上面的代码有欠考虑

#include<iostream>
#include<vector>
using namespace std;
int main() {
const int MAX = 100000;
int n, m, a, b, c;
while (cin >> n >> m) {
vector<vector<int>> map(n, vector<int>(n, MAX));
for (int i = 0; i < m; ++i) {
cin >> a >> b >> c;
map[a][b] = map[b][a] = c;
}
cin >> a >> b;
vector<int> cost = map[a];
for (int k = 0; k < n; ++k)
for (int j = 0; j < n; ++j)
if (cost[k] + map[k][j] < cost[j])
cost[j] = cost[k] + map[k][j];
cout << (cost[b] == MAX ? -1 : cost[b]) << endl;
}
return 0;
}
Italink 2019-08-20
  • 打赏
  • 举报
回复
既然固定起点,那么就不需要用三层循环完成Floyd算法了

#include<iostream>
#include<vector>
using namespace std;
int main() {
int n, m, a, b, c;
while (cin >> n >> m) {
vector<vector<int>> map(n, vector<int>(n,-1));
for (int i = 0; i < m; ++i) {
cin >> a >> b >> c;
map[a][b] = map[b][a] = c;
}
cin >> a >> b;
vector<int> cost = map[a];
for (int k = 0; k < n; ++k)
for (int j = 0; j < n; ++j)
if (cost[k]>0&&map[k][j]>0&&cost[k] + map[k][j] < cost[j])
cost[j] = cost[k] + map[k][j];
cout << cost[b] << endl;
}
return 0;
}
木槿槿槿 2019-08-20
  • 打赏
  • 举报
回复
引用 2 楼 Italink的回复:
上面的代码有欠考虑

#include<iostream>
#include<vector>
using namespace std;
int main() {
const int MAX = 100000;
int n, m, a, b, c;
while (cin >> n >> m) {
vector<vector<int>> map(n, vector<int>(n, MAX));
for (int i = 0; i < m; ++i) {
cin >> a >> b >> c;
map[a][b] = map[b][a] = c;
}
cin >> a >> b;
vector<int> cost = map[a];
for (int k = 0; k < n; ++k)
for (int j = 0; j < n; ++j)
if (cost[k] + map[k][j] < cost[j])
cost[j] = cost[k] + map[k][j];
cout << (cost[b] == MAX ? -1 : cost[b]) << endl;
}
return 0;
}
谢谢 我只是在学它的时候做了这个题 算是对它的练习

15,447

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧