zoukankan      html  css  js  c++  java
  • [POJ1797] Heavy Transportation(最大生成树 || 最短路变形)

    传送门

    1.最大生成树

      可以求出最大生成树,其中权值最小的边即为答案。

    2.最短路

      只需改变spfa里面的松弛操作就可以求出答案。

    ——代码

     1 #include <queue>
     2 #include <cstdio>
     3 #include <cstring>
     4 
     5 using namespace std;
     6 
     7 const int MAXN = 1005;
     8 int T, n, m, cnt;
     9 int head[MAXN], next[MAXN * MAXN], to[MAXN * MAXN], val[MAXN * MAXN], dis[MAXN];
    10 bool vis[MAXN];
    11 queue <int> q;
    12 
    13 inline void add(int x, int y, int z)
    14 {
    15     to[cnt] = y;
    16     val[cnt] = z;
    17     next[cnt] = head[x];
    18     head[x] = cnt++;
    19 }
    20 
    21 inline void spfa(int u)
    22 {
    23     int i, v;
    24     memset(dis, 0, sizeof(dis));
    25     memset(vis, 0, sizeof(vis));
    26     while(!q.empty()) q.pop();
    27     q.push(u);
    28     vis[u] = 1;
    29     dis[u] = 0x3f3f3f3f;
    30     while(!q.empty())
    31     {
    32         u = q.front();
    33         q.pop();
    34         vis[u] = 0;
    35         for(i = head[u]; i != -1; i = next[i])
    36         {
    37             v = to[i];
    38             if(min(dis[u], val[i]) > dis[v])
    39             {
    40                 dis[v] = min(dis[u], val[i]);
    41                 if(!vis[v])
    42                 {
    43                     q.push(v);
    44                     vis[v] = 1;
    45                 }
    46             }
    47         }
    48     }
    49 }
    50 
    51 int main()
    52 {
    53     int i, j, x, y ,z;
    54     scanf("%d", &T);
    55     for(i = 1; i <= T; i++)
    56     {
    57         scanf("%d %d", &n, &m);
    58         cnt = 0;
    59         memset(head, -1, sizeof(head));
    60         for(j = 1; j <= m; j++)
    61         {
    62             scanf("%d %d %d", &x, &y, &z);
    63             add(x, y, z);
    64             add(y, x, z);
    65         }
    66         spfa(1);
    67         printf("Scenario #%d:
    ", i);
    68         printf("%d
    
    ", dis[n]);
    69     }
    70     return 0;
    71 }
    View Code
  • 相关阅读:
    检查使用的端口
    time is always agains us
    检查使用的端口
    dreque问题一例
    查看重定向的输出
    安装VSS时,Um.dat may be corrupt
    修改网卡ip
    redis install on ubuntu/debian
    上火了
    学这么多技术是为什么
  • 原文地址:https://www.cnblogs.com/zhenghaotian/p/6811773.html
Copyright © 2011-2022 走看看