Til the Cows Come Home
来源:互联网 发布:mac mini机箱 编辑:程序博客网 时间:2024/06/05 23:56
Think:
单源最短路问题采用Dijkstra算法TAT。 套模板就好了。。。。
整整写了一个小时感觉没救了, 10个WA, 1个PE 呵呵。
题目大意:
有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离
注意点:
判断重边
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.
Output - Line 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<iostream>#include<cstdio>#include<string.h>#define INF 0x3f3f3f3f#include<math.h>#include<algorithm>#include<stdlib.h>#include<stdio.h>8using namespace std;bool vis[10086];int Map[3030][3030];int dis[3030];int Dijkstra();int N, T;int main(){ int i, j; int a, b, c; cin >> T >> N; memset(vis, 0, sizeof(vis)); memset(dis, 0, sizeof(dis)); for(i = 1; i <= N; i ++) { for (j = 1; j <= N; j ++) { Map[i][j] = INF; } Map[i][i] = 0; } for (i = 1; i <= N; i ++) dis[i] = INF; dis[N] = 0; while(T --) { cin >> a >> b >> c; if (Map[a][b] > c) Map[a][b] = Map[b][a] = c; } int ans = Dijkstra(); cout << ans << endl;}int Dijkstra(){ int i, j, k; for (i = 1; i <= N; i ++) { int u, MIN = INF; for (j = 1; j <= N; j ++) { if (!vis[j] && dis[j] <= MIN) { u = j; MIN = dis[j]; } } vis[u] = 1; for(k = 1; k <= N; k ++) { dis[k] = min(dis[k], dis[u] + Map[u][k]); } } return dis[1];}
- 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
- POJ 2387 Til the Cows Come Home
- PKU2387 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
- DPDK-网络收发包平台优化及配置优化
- JS刷新一次页面,第二次显示样式
- 对数据库设计的三大范式的理解
- 【HDU 6033 】Add More Zero 【思维】
- JS中script标签defer和async属性
- Til the Cows Come Home
- Java数据结构与算法
- 备忘 mfc中caffe遇到的相关问题
- DecimalFormat的用法记录
- win10 64bit 安装numpy,scipy
- 开始我的博客之旅
- 数值统计
- C语言实现MD5算法
- 通俗易懂的进程与线程解释