Til the Cows Come Home (dijkstra)
来源:互联网 发布:stl文件编辑软件 编辑:程序博客网 时间:2024/06/11 23:46
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.
Til the Cows Come Home
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.
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.
* 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.
* 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.
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
5 51 2 202 3 303 4 204 5 201 5 100
90
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
code:
#include <iostream>#include <cstring>#include <cstdio>using namespace std;#define INF 0x3f3f3f3fint mp[1001][1001];int dis[1001];int vis[1001];int t,n;void dijkstra(){int i;memset(vis,0,sizeof(vis));for(i = 1; i <= n; i++){dis[i] = mp[1][i];}dis[1] = 0;vis[1] = 1;int j;for(i = 1; i < n; i++){int u = -1;int mind = INF;for(j = 1; j <= n; j++){if(!vis[j] && dis[j] < mind){mind = dis[u=j];}}if(u == -1)break;vis[u] = 1;for(j = 1; j <= n; j++){if(!vis[j] && dis[j] > dis[u] + mp[u][j]) dis[j] = dis[u] + mp[u][j];}}cout << dis[n] << endl;}int main(){cin >> t >> n;int i,j;memset(mp,INF,sizeof(mp));for(i = 0; i < t; i++){int a,b,d;cin >> a >> b >> d;if(mp[a][b] > d){mp[a][b] = d;mp[b][a] = d;}}dijkstra();return 0;}
阅读全文
0 0
- POJ2387 Til the Cows Come Home【Dijkstra】
- POJ2387----Til the Cows Come Home(dijkstra)
- Til the Cows Come Home (Dijkstra)
- Til the Cows Come Home (dijkstra)
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- Til the Cows Come Home
- POJ2387 Til the Cows Come Home——Heap+dijkstra
- springMVC+mybatis整合之applicationContext.xml基本配置详解
- [Grafana--监控工具]--介绍
- 【Educational Codeforces Round 33】 D. Credit Card (贪心)
- 子网掩码
- 设计模式之单例模式
- Til the Cows Come Home (dijkstra)
- 从边缘计算到人工智能 新技术加速更迭的迷茫与思考
- 数据类型与变量的分析
- 获取当前应用的版本号和当前android系统的版本号
- tensorflow的hellow world:mnist手写识别
- kettle连接数据库问题
- sql的常用技巧
- phpstorm用正则删除PHP代码空行小技巧
- linux redhat6.2 图形化界面黑屏修改时间