hdu 1874 暢通工程續【裸最短路】

來源:互聯網
上載者:User
連結:

http://acm.hdu.edu.cn/showproblem.php?pid=1874

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=29015#problem/E

暢通工程續

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20363    Accepted Submission(s): 7056


Problem Description某省自從實行了很多年的暢通工程計劃後,終於修建了很多路。不過路多了也不好,每次要從一個城鎮到另一個城鎮時,都有許多種道路方案可以選擇,而某些方案要比另一些方案行走的距離要短很多。這讓行人很困擾。

現在,已知起點和終點,請你計算出要從起點到終點,最短需要行走多少距離。

 


Input本題目包含多組資料,請處理到檔案結束。
每組資料第一行包含兩個正整數N和M(0<N<200,0<M<1000),分別代表現有城鎮的數目和已修建的道路的數目。城鎮分別以0~N-1編號。
接下來是M行道路資訊。每一行有三個整數A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城鎮A和城鎮B之間有一條長度為X的雙向道路。
再接下一行有兩個整數S,T(0<=S,T<N),分別代表起點和終點。 


Output對於每組資料,請在一行裡輸出最短需要行走的距離。如果不存在從S到T的路線,就輸出-1. 


Sample Input

3 30 1 10 2 31 2 10 23 10 1 11 2
 


Sample Output

2-1
 


Authorlinle 


Source2008浙大研究生複試熱身賽(2)——全真類比 


Recommendlcy

code:
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;const int maxn = 210;const int INF = 210*110;int w[maxn][maxn];int d[maxn];int vis[maxn];int n,m;int s,t;void Dijkstra(){    for(int i = 0; i < n; i++) d[i] = w[s][i];    d[s] = 0;    memset(vis,0,sizeof(vis));    for(int i = 1; i <= n; i++)    {        int x,m = INF;        for(int y = 0; y < n; y++) if(!vis[y] && d[y] <= m) m = d[x=y];        vis[x] = 1;        for(int y = 0; y < n; y++)            d[y] = min(d[y], d[x]+w[x][y]);    }}int main(){    while(scanf("%d%d", &n,&m) != EOF)    {        for(int i = 0; i < n; i++)        {            for(int j = 0; j < n; j++)                w[i][j] = (i == j ? 0 : INF);        }        for(int i = 0; i < m; i++)        {            int a,b,x;            scanf("%d%d%d", &a,&b,&x);            w[a][b] = min(w[a][b], x);            w[b][a] = w[a][b];        }        scanf("%d%d", &s,&t);        Dijkstra();        if(d[t] != INF) printf("%d\n", d[t]); //注意輸出        else printf("-1\n");    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.