【POJ - 2387】 Til the Cows Come Home(单源最短路Dijkstra算法)
題干:
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 100Sample Output
90Hint
INPUT DETAILS:?
There are five landmarks.?
OUTPUT DETAILS:?
Bessie can get home by following trails 4, 3, 2, and 1.
解題報(bào)告:
? ? 單源最短路裸題。
AC代碼:?
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream>using namespace std; const int MAX = 100000 + 5; const int INF = 0x3f3f3f3f ; int vis[MAX]; int maze[1005][1005]; int n,m; int dis[MAX];//表示從出發(fā)點(diǎn)開始到該點(diǎn)的最短距離。 void Dijkstra(int u,int v) {dis[u] = 0; // vis[u] = 1; //這步還真不能加上。。。。。 int minw = INF,minv;for(int k = 1;k<= n;k++) {minw = INF;for(int i = 1; i<=n; i++) {if(vis[i] ) continue;if(dis[i] < minw) {minv = i; minw = dis[i];}}vis[minv] = 1;if(minv == v) break;for(int i = 1; i<=n; i++) {if(vis[i]) continue;if(maze[minv][i] == 0) continue;dis[i] = min(dis[i], dis[minv] + maze[minv][i]);}}printf("%d\n",dis[v]);} int main() {int a,b,w;while(~scanf("%d %d",&m,&n) ) {memset(dis,0x3f3f3f3f,sizeof(dis) ) ;memset(maze,0x3f3f3f3f,sizeof(maze) );memset(vis,0,sizeof(vis) ) ;for(int i = 1; i<=m; i++) {scanf("%d%d%d",&a,&b,&w);if(w<maze[a][b])maze[a][b] = maze[b][a] = w;}Dijkstra(1,n); }return 0 ;}AC代碼2:(Bellman-Ford算法)
//*bellman算法: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define N 2010 #define MAX 99999999 using namespace std ; struct node {int a , b , w ; } edge[N]; int n , m ; void bell() {int i , j ;int d[N];for(int i =1 ; i<=n; i++) { //*距離初始化為無窮;d[i]=MAX;}d[1]=0;//*初始地點(diǎn)為0;for(i=1; i<=n; i++) {for(j=1; j<=m; j++) { //*按點(diǎn)-邊搜,順便解決了重邊問題;if(d[edge[j].a]>d[edge[j].b]+edge[j].w) d[edge[j].a]= d[edge[j].b]+edge[j].w;if(d[edge[j].b]>d[edge[j].a]+edge[j].w) d[edge[j].b]= d[edge[j].a]+edge[j].w;}}printf("%d\n",d[n]); } int main() {int i , a , b ,c;while(cin>>m>>n) {for(int i =1 ; i<=m; i++) { //*結(jié)構(gòu)體存邊和權(quán)cin>>a>>b>>c;edge[i].a=a;edge[i].b=b;edge[i].w=c;}bell();}return 0 ; }?
?總結(jié):
? ? 別忘處理一下重邊!
總結(jié)
以上是生活随笔為你收集整理的【POJ - 2387】 Til the Cows Come Home(单源最短路Dijkstra算法)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3个不买RTX 3080的理由:没钱只能
- 下一篇: silent.exe - silent是