【POJ】[2387]Til the Cows Come Home
来源:互联网 发布:淘宝联盟怎么推广赚钱 编辑:程序博客网 时间:2024/06/07 22:49
运用了最基础版的Dijkstra算法
还是很生疏……
很简单的最短路问题
题目说的那么多背景……
其实就是给各个路的权
然后求从1~N的最短路
需要注意有重复边 所以加个判断保留最短的权
多组测试数据……
#include<stdio.h>int e[1200][1200],dis[1200];int main() { int inf=999999; int T,n; while(scanf("%d %d",&T,&n)!=EOF) { for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { if(i==j) e[i][j]=0; else e[i][j]=inf; } } while(T--) { int x,y,t; scanf("%d %d %d",&x,&y,&t); if(e[x][y]>t) e[x][y]=e[y][x]=t; } for(int i=1; i<=n; i++) dis[i]=e[1][i]; bool flag[1200]; for(int i=2; i<=n; i++) flag[i]=false; flag[1]=true; for(int i=1; i<=n; i++) { int min=inf,tj; for(int j=1; j<=n; j++) { if(!flag[j]&&dis[j]<min) { min=dis[j]; tj=j; } } flag[tj]=true; for(int k=1; k<=n; k++) { if(e[tj][k]<inf) { if(dis[k]>dis[tj]+e[tj][k]) dis[k]=dis[tj]+e[tj][k]; } } } printf("%d\n",dis[n]); } return 0;}
题目地址:【POJ】Til the Cows Come Home
0 0
- POJ 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- Poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ-2387-Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- DDL语句操作数据库
- 周期串
- IDEA ant连接ftp报错NoClassDefFoundError
- Light OJ 1421 Wavio Sequence (简单DP)
- MFC基础,MFC自绘控件学习总结.
- 【POJ】[2387]Til the Cows Come Home
- VM12即VMware Workstation 12 序列号
- 数据离散化
- 关于C语言include尖括号和双引号的对话
- 栈
- 1.1.3:可视化的累加器
- java高性能 tcp协议代码示例(一)
- android 音乐播放bug
- ACM_普通DP