HDU 2112

HDU Today

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 23058    Accepted Submission(s): 5517


Problem Description
经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强。这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬浦镇陶姚村买了个房子,开始安度晚年了。
这样住了一段时间,徐总对当地的交通还是不太了解。有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格)。
徐总经常会问蹩脚的英文问路:“Can you help me?”。看着他那迷茫而又无助的眼神,热心的你能帮帮他吗?
请帮助他用最短的时间到达目的地(假设每一路公交车都只在起点站和终点站停,而且随时都会开)。
 

Input
输入数据有多组,每组的第一行是公交车的总数N(0<=N<=10000);
第二行有徐总的所在地start,他的目的地end;
接着有n行,每行有站名s,站名e,以及从s到e的时间整数t(0<t<100)(每个地名是一个长度不超过30的字符串)。
note:一组数据中地名数不会超过150个。
如果N==-1,表示输入结束。
 

Output
如果徐总能到达目的地,输出最短的时间;否则,输出“-1”。
 

Sample Input
  
  
6 xiasha westlake xiasha station 60 xiasha ShoppingCenterofHangZhou 30 station westlake 20 ShoppingCenterofHangZhou supermarket 10 xiasha supermarket 50 supermarket westlake 10 -1
 

Sample Output
  
  
50 Hint: The best route is: xiasha->ShoppingCenterofHangZhou->supermarket->westlake 虽然偶尔会迷路,但是因为有了你的帮助 **和**从此还是过上了幸福的生活。 ――全剧终――
 


题目大意就不需要讲了吧



思路:

我们用map来保存值,然后其他的就简单了。。。


表示刚开始题目看错了,还以为这道题目要输出路径(嘟囔了一句怎么那么麻烦),于是花了很长时间想着该如何输出。。。(注释掉的部分就是打算路径输出的部分,不过还没有写完的时候就发现了,不过已经也算是写的差不多了)

然后这里有几个trick

①公交车的行走竟然是无向图,我也是醉了。不是应该不能掉头只能走一趟的吗

②起点和终点可能是重合的


#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
#include
       
       
         #include 
        
          #include 
          using namespace std; const int inf = 2000000000; const int maxn = 10000 + 5; struct Edge{ int from, to; int t; Edge(int from, int to, int t): from(from), to(to), t(t){} }; struct node{ int u, t; node(int u, int t): u(u), t(t){} bool operator < (const node &a) const { return t > a.t; } }; vector 
          
            e; vector 
           
             G[maxn]; int n, cnt; int sta, las; int par[maxn]; int d[maxn]; bool vis[maxn]; map 
            
              m; //map 
             
               ind; void init(){ m.erase(m.begin(), m.end()); e.erase(e.begin(), e.end()); cnt = 0; string ch1, ch2; cin >> ch1; m[ch1] = ++cnt; sta = cnt; //ind[cnt] = ch1; cin >> ch2; if (ch2 != ch1) ++cnt; m[ch2] = cnt; las = cnt; //ind[cnt] = ch2; for (int i = 1; i <= n; i++){ int u, v, tmp; cin >> ch1 >> ch2 >> tmp; if (m[ch1] == 0){ m[ch1] = ++cnt; } u = m[ch1]; //ind[u] = ch1; if (m[ch2] == 0){ m[ch2] = ++cnt; } v = m[ch2]; //ind[v] = ch2; e.push_back(Edge(u, v, tmp)); e.push_back(Edge(v, u, tmp)); int l = e.size(); G[u].push_back(l - 2); G[v].push_back(l - 1); } } void dijkstra(int s){ priority_queue 
              
                que; for (int i = 0; i <= cnt; i++){ d[i] = inf; vis[i] = false; par[i] = i; } d[1] = 0; que.push(node(s, d[1])); while (!que.empty()){ node x = que.top(); que.pop(); int u = x.u; if (vis[u]) continue; vis[u] = true; for (int i = 0; i < G[u].size(); i++){ Edge& y = e[G[u][i]]; if (d[y.to] > d[u] + y.t){ d[y.to] = d[u] + y.t; par[y.to] = G[u][i]; que.push(node(y.to, d[y.to])); } } } if (d[las] == inf){ d[las] = -1; } printf("%d\n", d[las]); /*vector 
               
                 s; for (int i = par[2]; i != par[i]; i = par[i]){ string ch = ind[i]; s.push_back(ch); } reverse(s.begin(), s.end()); for ()*/ } int main(){ while (scanf("%d", &n) && n != -1){ init(); dijkstra(sta); for (int i = 1; i <= cnt; i++){ G[i].erase(G[i].begin(), G[i].end()); } } return 0; } 
                
               
              
             
            
           
         
       
      
      
     
     
    
    
   
   







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值