• hdu2544 (bellman-ford)


    题意:求顶点 1 - 顶点n 之间的最短路

    解题思路:bellman-ford 算法,利用n-1松弛操作得到最短路,算法复杂度为 V*E

    解题代码:

     1 // File Name: 2544_1.cpp
     2 // Author: darkdream
     3 // Created Time: 2014年04月04日 星期五 22时37分45秒
     4 
     5 #include<vector>
     6 #include<list>
     7 #include<map>
     8 #include<set>
     9 #include<deque>
    10 #include<stack>
    11 #include<bitset>
    12 #include<algorithm>
    13 #include<functional>
    14 #include<numeric>
    15 #include<utility>
    16 #include<sstream>
    17 #include<iostream>
    18 #include<iomanip>
    19 #include<cstdio>
    20 #include<cmath>
    21 #include<cstdlib>
    22 #include<cstring>
    23 #include<ctime>
    24 #include<climits>
    25 
    26 using namespace std;
    27 
    28 struct node{
    29     int x, d ; 
    30     node()
    31     {}
    32     node(int p,int q)
    33     {
    34         x = p; 
    35         d = q; 
    36     }
    37 };
    38 vector <node> edge[200];
    39 int n , m ; 
    40 int dis[200];
    41 int bellman_ford(int s)
    42 {
    43     for(int i = 1;i <= n;i ++) dis[i] = 1 << 27; 
    44     dis[s] = 0 ; 
    45     for(int i = 1;i <= n -1;i ++)
    46     {
    47         for(int j = 1;j <= n;j ++)
    48         {
    49             int k = edge[j].size();
    50             for(int p = 0 ;p < k ;p ++)
    51             {
    52                 if(dis[edge[j][p].x] > dis[j] + edge[j][p].d)
    53                     dis[edge[j][p].x] = dis[j] + edge[j][p].d; 
    54             }
    55         }
    56     }
    57     return 1;   
    58 }
    59 int main(){
    60     //freopen("/home/plac/problem/input.txt","r",stdin);
    61     //freopen("/home/plac/problem/output.txt","w",stdout);
    62     while(scanf("%d %d",&n,&m)!= EOF)
    63     {
    64         if(n == 0 && m == 0 )
    65             break;
    66         for(int i =1 ;i <= n;i++)
    67             edge[i].clear();
    68         for(int i = 1;i <= m ;i ++)
    69         { 
    70             int a, b, c;
    71             scanf("%d %d %d",&a,&b,&c);
    72             edge[a].push_back(node(b,c));
    73             edge[b].push_back(node(a,c));
    74         }
    75         if(bellman_ford(1))
    76             printf("%d
    ",dis[n]);
    77     }
    78     return 0;
    79 }
    View Code
    没有梦想,何谈远方
  • 相关阅读:
    什么是内卷?
    iphone与PC端如何传BUG截图
    java应用服务占用cpu过高,如何优化
    性能测试常见问题FAQ
    性能测试工程师能力进阶三部曲
    jmeter分布式压测试部署
    了解token及分类
    常见端口号及其服务
    2714
    python
  • 原文地址:https://www.cnblogs.com/zyue/p/3646385.html
Copyright © 2020-2023  润新知