HDU 2680 Choose the best route
来源:互联网 发布:拍婚纱照攻略知乎 编辑:程序博客网 时间:2024/06/05 02:33
题意:求几个点到到终点那个的距离最小,输出该值,若不存在,输出-1.注意:这题连个相同的点的距离为无限大。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define N 1005const int inf=1<<29;int vis[N],w[N][N],d[N];int n,m,s;void dij(int s){ int i,j; for(i=1;i<=n;i++) { d[i]=w[s][i]; } d[s]=0; vis[s]=1; for(i=1;i<=n;i++) { int now=inf,k; for(j=1;j<=n;j++) { if(!vis[j]&&d[j]<now) { now=d[j]; k=j; } } if(now==inf)//当没更新,直接停止 { break; } vis[k]=1; for(j=1;j<=n;j++) { if(!vis[j]&&d[j]>d[k]+w[k][j]) { d[j]=d[k]+w[k][j]; } } }}int main(){ while(scanf("%d%d%d",&n,&m,&s)!=EOF) { int i,j; memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { w[i][j]=inf;//这题超级坑,两个相同的点之间的距离为无限大 } } int u,v,c; while(m--) { scanf("%d%d%d",&u,&v,&c); if(c<w[v][u])//值得注意,倒着求,让起点为终点,终点为起点 { w[v][u]=c; } } dij(s); int t,r,MIN=inf; scanf("%d",&t); for(i=1;i<=t;i++) { scanf("%d",&r); MIN=min(MIN,d[r]);//求几个距离的最小值 } if(MIN==inf) { printf("-1\n"); } else { printf("%d\n",MIN); } } return 0;}
0 0
- HDU 2680 Choose the best route
- HDU-2680 choose the best route
- Hdu-2680 Choose the best route
- hdu 2680 Choose the best route
- HDU 2680 Choose the best route
- HDU 2680-Choose the best route
- hdu 2680 Choose the best route
- hdu 2680 Choose the best route
- hdu 2680 Choose the best route
- HDU 2680 Choose the best route
- HDU 2680 Choose the best route(dijkstra)
- hdu 2680 Choose the best route
- hdu 2680 Choose the best route
- hdu 2680 Choose the best route
- hdu 2680 Choose the best route
- hdu 2680 Choose the best route
- hdu 2680 Choose the best route
- hdu 2680 Choose the best route
- Java动态代理
- Hadoop学习资料整理收集----阿冬专栏
- 音视频同步(播放)原理
- Android Studio 常用设置
- cmd 连接SQLServer命令
- HDU 2680 Choose the best route
- Scrapy安装
- 为android系统添加产品的过程
- 字符转整型
- 存储过程,存储过程循环,一张表整列插入另一张表
- PC-Lint详解
- 探索 Pexpect,第 1 部分:剖析 Pexpect
- SQL SERVER 生成表结构的语句
- Ubuntu Server 将环境变量修改为英文,防止中文乱码