码迷,mamicode.com
首页 > 其他好文 > 详细

【hdu 2112】 HDU Today ( 最短路 Dijkstra)(map)

时间:2015-11-17 16:37:15      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

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

 

这道题给了一个将字符串与int对应的思路,就是使用map

 

这道题答案对了,但是没有AC,我也不知道为什么。。

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <string>
 4 #include <queue>
 5 #include <vector>
 6 #include <map>
 7 using namespace std;
 8 const int Ni = 10000;
 9 const int INF = 1<<27;
10 
11 typedef pair<int,int> pa;
12 
13 int dis[Ni],n;//dis使用1~n的部分
14 
15 vector<pair<int,int> > eg[Ni];
16 
17 void Dijkstra(int s)
18 {
19           int i,j;
20           cout<<n<<endl;
21           for(i=0;i<=n;i++)//要到n
22                     dis[i] = INF;
23 
24           priority_queue<pa>q;  //优先级队列:小顶堆
25           dis[s] = 0;
26           q.push(make_pair(s,dis[s]));
27           while(!q.empty())
28           {
29                     pa x = q.top();
30                     q.pop();
31                     int w = x.first;
32                     for(j = 0;j<eg[w].size();j++)//遍历x的所有邻接点
33                     {
34                               pa y = eg[w][j];//y是x的邻接点
35                               int u = y.first;
36                               if(dis[u]>x.second+y.second)
37                               {
38                                         dis[u] = x.second+y.second;
39                                         q.push(make_pair(u,dis[u]));
40                               }
41                     }
42           }
43 
44 }
45 
46 
47 int main()
48 {
49           int m,d;//关系个数
50           string a,b;
51           map<string,int> mp;
52           while(cin>>n && n!=-1)
53           {
54                     mp.clear();
55                     for(int i = 0;i<=n;i++)
56                               eg[i].clear();//初始化
57                     cin>>a>>b;
58                     bool flag = false;
59                     if(a==b)
60                               flag = true;
61                     mp[a] = 1;
62                     mp[b] = 2;
63                     int k = 3;
64                     int w,u;
65                     int t = n;
66                     while(t--)
67                     {
68                               cin>>a>>b>>d;
69                               if(!mp[a])
70                               {
71                                         w = k;
72                                         mp[a] = k++;
73                               }
74                               if(!mp[b])
75                               {
76                                         u = k;
77                                         mp[b] = k++;
78                               }
79                               eg[mp[a]].push_back(make_pair(mp[b],d));
80                               eg[mp[b]].push_back(make_pair(mp[a],d));
81                     }
82                     if(flag)
83                     {
84                               cout<<"0\n";
85                               continue;
86                     }
87                     Dijkstra(1);
88                     if(dis[2]!=INF)
89                               cout<<dis[2]<<endl;
90                     else
91                               cout<<"-1\n";
92           }
93 
94           return 0;
95 }

 

【hdu 2112】 HDU Today ( 最短路 Dijkstra)(map)

标签:

原文地址:http://www.cnblogs.com/qlky/p/4971840.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!