• UVA 10462 Is There A Second Way Left? (次小生成树+kruskal)


    题目大意:

      Nasa应邻居们的要求,决定用一个网络把大家链接在一起。给出v个点,e条可行路线,每条路线分别是x连接到y需要花费w。

      1:如果不存在最小生成树,输出“No way”.

      2:如果不存在次小生成树,输出“No second way”.

      3:如果两者都存在,输出次小生成树的长度.

    解题思路:

      次小生成数+kruskal模板

      1 #include <cmath>
      2 #include <queue>
      3 #include <string>
      4 #include <cstdio>
      5 #include <cstring>
      6 #include <iostream>
      7 #include <algorithm>
      8 using namespace std;
      9 
     10 const int maxn = 210;
     11 const int INF = 0x3f3f3f3f;
     12 const double Exp = 1e-10;
     13 struct node
     14 {
     15     int x, y, w;
     16 
     17 };
     18 bool cmp (node a, node b)
     19 {
     20     return a.w < b.w;
     21 }
     22 node stu[maxn];
     23 int v, e, father[maxn], path[maxn];
     24 
     25 void init ()
     26 {
     27     for (int i=0; i<=v; i++)
     28         father[i] = i;
     29 }
     30 
     31 int find (int x)
     32 {
     33     if (father[x] != x)
     34         father[x] = find(father[x]);
     35     return father[x];
     36 }
     37 int kruskal ()
     38 {
     39     int i, j, num = 0;
     40     for (i=0,j=1; i<e; i++)
     41     {
     42         int px = find (stu[i].x);
     43         int py = find (stu[i].y);
     44         if (px != py)
     45         {
     46             num += stu[i].w;
     47             path[j++] = i;
     48             father[px] = py;
     49         }
     50     }
     51     int ans = 0;
     52     for (i=1; i<=v; i++)
     53         if (father[i] == i)
     54             ans ++;
     55     if (ans == 1)
     56         return num;
     57     return INF;
     58 }
     59 int smst (int x)
     60 {
     61     int i, num = 0;
     62     for (i=0; i<e; i++)
     63     {
     64         int px = find(stu[i].x);
     65         int py = find(stu[i].y);
     66         if (px != py && i != x)
     67         {
     68             num += stu[i].w;
     69             father[px] = py;
     70         }
     71     }
     72     int ans = 0;
     73     for (i=1; i<=v; i++)
     74         if (father[i] == i)
     75             ans ++;
     76     if (ans == 1)
     77         return num;
     78     return INF;
     79 }
     80 int main ()
     81 {
     82     int t, l = 1;
     83     scanf ("%d", &t);
     84     while (t --)
     85     {
     86         scanf ("%d %d", &v, &e);
     87         for (int i=0; i<e; i++)
     88             scanf ("%d %d %d", &stu[i].x, &stu[i].y, &stu[i].w);
     89         sort (stu, stu+e, cmp);
     90         int num1 , num2;
     91         num1 = num2 = INF;
     92         init ();
     93         num1 = kruskal();
     94         for (int i=1; i<v; i++)
     95         {
     96             init ();
     97             num2 = min (num2, smst(path[i]));
     98         }
     99         if (num1 == INF)
    100             printf ("Case #%d : No way
    ", l++);
    101         else if (num2 == INF)
    102             printf ("Case #%d : No second way
    ", l++);
    103         else
    104             printf ("Case #%d : %d
    ", l++, num2);
    105     }
    106     return 0;
    107 }
    本文为博主原创文章,未经博主允许不得转载。
  • 相关阅读:
    MySQL binlog_ignore_db 参数最全解析
    推荐几款MySQL相关工具
    MySQL从库维护经验分享
    swaggert随笔
    springBoot
    git-本地仓库操作
    springboot初识
    spring data jpa
    mybatis复习笔记----关于一些需记忆的点
    mybatis复习笔记----关于标签
  • 原文地址:https://www.cnblogs.com/alihenaixiao/p/4563667.html
Copyright © 2020-2023  润新知