A - 畅通工程续

某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。

现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。

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
 #include<bits/stdc++.h>
using namespace std; const int inf = 0x3f3f3f3f;
const int N = ;
const int M = ;
int n, m, s, t;
int dis[N][N]; void Floyd(){
for(int k=; k<n; k++)
for(int i=; i<n; i++)
for(int j=; j<n; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
} int main(){
while(~scanf("%d %d", &n, &m)){
//初始化
for(int i=; i<n; i++){
for(int j=; j<n; j++)
dis[i][j] = inf;
dis[i][i] = ;
}
//
int a, b, x;
for(int i=; i<=m; i++){
scanf("%d %d %d", &a, &b, &x);
dis[a][b] = dis[b][a] = min(dis[a][b], x);
}
//
Floyd();
//
scanf("%d %d", &s, &t);
if(dis[s][t] == inf) printf("-1\n");
else printf("%d\n",dis[s][t]);
}
}
												

最新文章

  1. 我常用的那些linux命令
  2. Lua 栈的理解
  3. AngularJS学习笔记1
  4. poj_2421_mst
  5. Struts2 Annotation 注解配置
  6. C# 数据类型映射 (SQLite,MySQL,MSSQL,Oracle)
  7. List迭代循环时出现分问题
  8. android中网络操作使用总结(http)
  9. php提取身份证号码中的生日日期以及验证是否为未成年人的函数
  10. poj 2965
  11. sudo brew install mongodb报错
  12. 使用Shader制作loading旋转动画
  13. Daily record-October
  14. 通过Java构造参数列表
  15. python爬虫-淘宝商品密码(图文教程附源码)
  16. yum被锁定:Another app is currently holding the yum lock; waiting for it to exit…
  17. send和sendmsg性能测试
  18. MS08_067漏洞测试——20145301
  19. PHP 包含文件
  20. js在IE8+兼容String没有trim方法,写一个兼容ie8一下的浏览器的trim()方法

热门文章

  1. Linux 性能分析 工具命令
  2. linux系统的启动流程梳理
  3. 珠峰-vue
  4. MySQL中的幻读,你真的理解吗?
  5. Mysql无法启动、闪退
  6. 字符串转数字 (With C++)
  7. 生成JavaDoc文档
  8. 9.16java总结
  9. Centos7 虚拟机安装增强功能
  10. 【终端使用】&quot;scp&quot;命令,远程拷贝文件