POJ-2387-Til the Cows Come Home(迪杰斯特拉)
来源:互联网 发布:mysql 查找所有子部门 编辑:程序博客网 时间:2024/06/14 13:51
Til the Cows Come Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 42680 Accepted: 14501
Description
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.
Farmer John’s field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Input
Line 1: Two integers: T and N
Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
OutputLine 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
Hint
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
裸的迪杰斯特拉代码
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<math.h>#include<queue>using namespace std;const int maxn=1005;const int INF=0x3f3f3f3f;int map[maxn][maxn];int dis[maxn];//记录最短路int vis[maxn];//初始化为0,已访问标记为1int T;//边的数量int N;//最大顶点编号,求N到1最短路void Dijkstra(){ memset(vis,0,sizeof(vis)); vis[1]=1; for(int i=1; i<=N; i++) dis[i]=map[1][i]; for(int i=1; i<N; i++) { int minn=INF; int point; for(int j=1; j<=N; j++) if(vis[j]==0&&dis[j]<minn) { minn=dis[j]; point=j; }// if(minn==INF)// return; vis[point]=1; for(int j=1; j<=N; j++) { if(map[point][j]<INF&&dis[j]>dis[point]+map[point][j]) dis[j]=dis[point]+map[point][j]; } }}int main(){ scanf("%d%d",&T,&N); for(int i=0; i<=N; i++) //初始化矩阵 for(int j=0; j<=N; j++) i==j?map[i][j]=0:map[i][j]=INF; while(T--) { int u,v,w; scanf("%d%d%d",&u,&v,&w); if(w<map[u][v]) { map[u][v]=w; map[v][u]=w; } } Dijkstra(); printf("%d\n",dis[N]); return 0;}
- POJ-2387-Til the Cows Come Home(迪杰斯特拉)
- POJ 2387Til 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
- SharePoint 2013 Foundation 无法连接到 SMTP 主机
- 【NOIP2016提高A组模拟7.15】计数 题解+代码
- Eclipse高效率开发技巧(慢慢完善中......)
- 清除缓存
- 关于wordPress4.5.x的主题安装时碰到的权限问题
- POJ-2387-Til the Cows Come Home(迪杰斯特拉)
- Android工具类之文件操作工具类
- android屏幕适配
- 一分钟帮你提升Android studio 编译速度
- Java中使用Jedis操作Redis
- Cocos2dx-3.x中常用切换场景特效和方式
- AsyncTask模拟小应用 AsyncTask步骤
- textview添加超链接
- [JZOJ4629] 修路