hdu 2544


最短路

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 50493    Accepted Submission(s): 22211


Problem Description
在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?

 

Input
输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
输入保证至少存在1条商店到赛场的路线。
 

Output
对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间
 

Sample Input
  
  
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
 

Sample Output
  
  
3 2
 


从简单的题目开始敲起

#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
#include
       
       
         using namespace std; typedef long long ll; const ll inf = 1e16; const int maxn = 200 + 5; int n, m; struct Edge{ int from, to; int val; Edge(int from, int to, int val): from(from), to(to), val(val){} }; vector 
        
          edges; vector 
         
           G[maxn]; struct node{ int u; ll d; node(int u, ll d): u(u), d(d){} bool operator < (const node &a) const{ return d > a.d; } }; ll d[maxn]; bool vis[maxn]; void init(){ edges.erase(edges.begin(), edges.end()); for (int i = 0; i <= n; i++){ G[i].erase(G[i].begin(), G[i].end()); } for (int i = 1; i <= m; i++){ int u, v, val; scanf("%d%d%d", &u, &v, &val); edges.push_back(Edge(u, v, val)); edges.push_back(Edge(v, u, val)); int l = edges.size(); G[u].push_back(l - 2); G[v].push_back(l - 1); } } void dijkstra(){ for (int i = 0; i <= n; i++){ vis[i] = false; d[i] = inf; } priority_queue 
          
            que; d[1] = 0; que.push(node(1, d[1])); while (!que.empty()){ node x = que.top(); que.pop(); int u = x.u; if (vis[u] == true) continue; vis[u] = true; for (int i = 0; i < G[u].size(); i++){ Edge y = edges[G[u][i]]; if (d[y.to] > d[u] + y.val){ d[y.to] = d[u] + y.val; que.push(node(y.to, d[y.to])); } } } printf("%I64d\n", d[n]); } int main(){ while (scanf("%d%d", &n, &m) && n + m != 0){ init(); dijkstra(); } return 0; } #include 
           
             #include 
            
              #include 
             
               #include 
              
                #include 
               
                 using namespace std; typedef long long ll; const ll inf = 1e16; const int maxn = 200 + 5; int n, m; ll d[maxn][maxn]; int main(){ while (scanf("%d%d", &n, &m) && n + m != 0){ for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ d[i][j] = inf; } } for (int i = 0; i < m; i++){ int u, v, val; scanf("%d%d%d", &u, &v, &val); if (val < d[u][v] && val < d[v][u]) d[u][v] = d[v][u] = val; } for (int i = 1; i <= n; i++){ d[i][i] = 0; } for (int k = 1; k <= n; k++){ for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } printf("%I64d\n", d[1][n]); } return 0; } #include 
                
                  #include 
                 
                   #include 
                  
                    #include 
                   
                     #include 
                    
                      using namespace std; typedef long long ll; const ll inf = 1e16; const int maxn = 200 + 5; int n, m; struct Edge{ int from, to, val; Edge(int from, int to, int val): from(from), to(to), val(val){} }; vector 
                     
                       edges; vector 
                      
                        G[maxn]; int cnt[maxn]; int vis[maxn]; ll d[maxn]; void init(){ edges.erase(edges.begin(), edges.end()); for (int i = 1; i <= n; i++){ G[i].erase(G[i].begin(), G[i].end()); cnt[i] = 0; vis[i] = false; d[i] = inf; } for (int i = 0; i < m; i++){ int u, v, d; scanf("%d%d%d", &u, &v, &d); edges.push_back(Edge(u, v, d)); edges.push_back(Edge(v, u, d)); int l = edges.size(); G[u].push_back(l - 2); G[v].push_back(l - 1); } } bool bellman(int s){ queue 
                       
                         que; que.push(s); d[s] = 0; vis[s] = true; while (!que.empty()){ int u = que.front(); que.pop(); vis[u] = false; for (int i = 0; i < G[u].size(); i++){ Edge &y = edges[G[u][i]]; if (d[y.to] > d[u] + y.val){ d[y.to] = d[u] + y.val; if (!vis[y.to]){ que.push(y.to); vis[y.to] = true; if (++cnt[y.to] == n) return false; } } } } return true; } int main(){ while (scanf("%d%d", &n, &m) && n + m != 0){ init(); bool flag = bellman(1); if (flag){ printf("%I64d\n", d[n]); } } return 0; } 
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值