hdu 2112 HDU Today (最短路)

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   資料   for   

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=2112

題目大意:給出起點和終點,然後算出最短的路。

不過有好多細節要注意:

(1)起始點和終止點相等的時候,這裡注意不能直接輸出0,必須用標記,因為資料可能還沒有處理完!!!此處貢獻n次wa。

(2)這裡是某大神教我的用map進行轉換,將字串轉換成數字,值得參考。map<string,int>M,V;不過這裡同樣是wa了好多次。要注意的是將最先輸入的開始和結束的點也要放到這個map裡面。

(3)再就是注意的dijkstra這個演算法的應用了。

祝大家好運~wa了14次的我眼淚掉下來。。。。。

  1 #include <iostream>  2 #include <cstdio>  3 #include <map>  4 #include <cstring>  5 #include <algorithm>  6 using namespace std;  7 #define INF 9999999  8 int k,Map[155][155],node[155];  9 map<string,int>M,V; 10  11 void set() 12 { 13     for (int i=0; i<155; i++) 14     { 15         node[i]=INF; 16         for (int j=0; j<155; j++) 17         { 18             if (i!=j) 19                 Map[i][j]=INF; 20             else 21                 Map[i][j]=0; 22         } 23     } 24 } 25  26 void dijkstra(int m) 27 { 28     int vis[155]= {0}; 29     int tm=m; 30     vis[m]=1; 31     node[m]=0; 32     for (int j=1; j<k; j++) 33     { 34         int Min=INF; 35         for (int i=0; i<k; i++) 36             if (!vis[i]) 37             { 38                 if (node[i]>node[tm]+Map[tm][i]) 39                     node[i]=node[tm]+Map[tm][i]; 40                 if (Min>node[i]) 41                 { 42                     Min=node[i]; 43                     m=i; 44                 } 45                 //cout<<i<<" "<<node[i]<<" "<<Map[tm][i]<<endl; 46             } 47         vis[m]=1; 48         tm=m; 49     } 50 } 51  52 int main() 53 { 54     bool flag; 55     int t,n; 56     while (scanf("%d",&n),n!=-1) 57     { 58         flag=0; 59         M.clear(); 60         V.clear(); 61         set(); 62         //memset(Map,INF,sizeof(Map)); 63         char start[135],end[135],s[135],e[135]; 64         k=2; 65         scanf("%s%s",start,end); 66         if (strcmp(start,end)==0) 67             flag=1; 68         if(n==0) 69         { 70             printf("-1\n"); 71             continue; 72         } 73         M[start]=0; 74         M[end]=1; 75         while (n--) 76         { 77             scanf("%s%s%d",s,e,&t); 78             if(!V[s]) 79             { 80                 V[s]=1; 81                 M[s]=k++; 82                 //cout<<k<<endl; 83             } 84             if(!V[e]) 85             { 86                 V[e]=1; 87                 M[e]=k++; 88                 //cout<<k<<endl; 89             } 90             if (Map[M[s]][M[e]]>t) 91                 Map[M[s]][M[e]]=Map[M[e]][M[s]]=t; 92         } 93         //set(); 94         //cout<<k<<endl; 95         if (flag) 96         { 97             printf ("0\n"); 98             continue; 99         }100         dijkstra(M[start]);101         if(node[M[end]]==INF)102             printf ("-1\n");103         else104             printf ("%d\n",node[M[end]]);105     }106     return 0;107 }
View Code

 

聯繫我們

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