日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 2387】 Til the Cows Come Home(单源最短路Dijkstra算法)

發布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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 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.

解題報告:

? ? 單源最短路裸題。

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];//表示從出發點開始到該點的最短距離。 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;//*初始地點為0;for(i=1; i<=n; i++) {for(j=1; j<=m; j++) { //*按點-邊搜,順便解決了重邊問題;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++) { //*結構體存邊和權cin>>a>>b>>c;edge[i].a=a;edge[i].b=b;edge[i].w=c;}bell();}return 0 ; }

?

?總結:

? ? 別忘處理一下重邊!

總結

以上是生活随笔為你收集整理的【POJ - 2387】 Til the Cows Come Home(单源最短路Dijkstra算法)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。