POJ-2387-Til the Cows Come Home

來源:互聯網
上載者:User

標籤:

問題描述

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.

輸入

* 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.

輸出

* 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

dijkstra的模板
 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 const int N=1001; 6 const int inf=1<<29; 7 int n,m,u,v,c; 8 int w[N][N],d[N]; 9 bool vis[N];10 void dij()11 {12     for(int i=1; i<=n; i++)13     {14         d[i]=inf;15     }16     d[1]=0;17     for(int i=0; i<n; i++)18     {19         int now=inf;20         int x;21         for(int j=1; j<=n; j++)22         {23             if(!vis[j]&&now>d[j])24             {25                 now=d[j];26                 x=j;27             }28         }29         vis[x]=1;30         for(int j=1; j<=n; j++)31         {32             if(!vis[j]&&d[j]>d[x]+w[x][j])33             {34                 d[j]=d[x]+w[x][j];35             }36         }37     }38 }39 int main()40 {41     while(scanf("%d%d",&m,&n)!=EOF)42     {43         memset(vis,0,sizeof(vis));44         for(int i=1; i<=n; i++)45         {46             for(int j=1; j<=n; j++)47             {48                 w[i][j]=inf;49             }50         }51         for(int i=1; i<=m; i++)52         {53             scanf("%d%d%d",&u,&v,&c);54             if(c<w[u][v])55                 w[u][v]=w[v][u]=c;56         }57         dij();58         printf("%d\n",d[n]);59     }60     return 0;61 }

 

POJ-2387-Til the Cows Come Home

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.