zoukankan      html  css  js  c++  java
  • POJ 2387 Til the Cows Come Home

    题目大意:

    给你N个点 T条边, 求N->1的最短路

     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <queue>
     7 using namespace std;
     8 #define INF 0xfffffff
     9 #define maxn 1002
    10 
    11 struct Edge
    12 {
    13     int e, w;
    14 };
    15 
    16 vector<Edge> G[1002];
    17 bool vis[maxn];
    18 int dist[maxn], n, m;;
    19 
    20 void Init()
    21 {
    22     for(int i=0; i<=n; i++)
    23     {
    24         G[i].clear();
    25         dist[i] = INF;
    26         vis[i] = false;
    27     }
    28 }
    29 int Spfa()
    30 {
    31     Edge P, Pn;
    32     queue<Edge> Q;
    33     P.e = n, P.w = 0;
    34     dist[n] = 0;
    35     Q.push(P);
    36 
    37     while( !Q.empty() )
    38     {
    39         P = Q.front();
    40         Q.pop();
    41 
    42         vis[P.e] = false;
    43         int len = G[P.e].size();
    44 
    45         for(int i=0; i<len; i++)
    46         {
    47             Pn = G[P.e][i];
    48 
    49             if(dist[Pn.e] > dist[P.e] + Pn.w)
    50             {
    51                 dist[Pn.e] = dist[P.e] + Pn.w;
    52 
    53                 if( !vis[Pn.e] )
    54                 {
    55                     Q.push(Pn);
    56                     vis[Pn.e] = true;
    57                 }
    58             }
    59         }
    60     }
    61     return dist[1];
    62 }
    63 int main()
    64 {
    65     Edge P;
    66 
    67     while(cin >> m >> n)
    68     {
    69         Init();
    70         for(int i=0; i<m; i++)
    71         {
    72             int a, b ,c;
    73 
    74             scanf("%d%d%d",&a,&b,&c);
    75 
    76             P.e = b, P.w = c;
    77             G[a].push_back(P);
    78             P.e = a;
    79             G[b].push_back(P);
    80         }
    81 
    82         int ans = Spfa();
    83 
    84         cout << ans << endl;
    85     }
    86     return 0;
    87 }
  • 相关阅读:
    智能指针之 auto_ptr
    UML在线绘图
    inline使用
    工作随笔—2017-12-12
    链表排序
    转——浅谈如何提高服务器并发处理能力
    使用re开发python计算器
    Linux-centos7下python3 环境设置
    C语言中的static 详细分析
    pycharm import pygame 出现报错:No module named 'pygame'
  • 原文地址:https://www.cnblogs.com/chenchengxun/p/4164211.html
Copyright © 2011-2022 走看看