zoukankan      html  css  js  c++  java
  • HDU 1879 继续畅通工程

    继续畅通工程

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 1879
    64-bit integer IO format: %I64d      Java class name: Main
     
     
    省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全省畅通需要的最低成本。
     

    Input

    测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( 1< N < 100 );随后的 N(N-1)/2 行对应村庄间道路的成本及修建状态,每行给4个正整数,分别是两个村庄的编号(从1编号到N),此两村庄间道路的成本,以及修建状态:1表示已建,0表示未建。

    当N为0时输入结束。
     

    Output

    每个测试用例的输出占一行,输出全省畅通需要的最低成本。
     

    Sample Input

    3
    1 2 1 0
    1 3 2 0
    2 3 4 0
    3
    1 2 1 0
    1 3 2 0
    2 3 4 1
    3
    1 2 1 0
    1 3 2 1
    2 3 4 1
    0

    Sample Output

    3
    1
    0

    Source

     
    解题:最小生成树。把已经修好的路边权设为0.
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 struct arc{
    18     int u,v,w;
    19     arc(int x = 0,int y = 0,int z = 0){
    20         u = x;
    21         v = y;
    22         w = z;
    23     }
    24 };
    25 int n,m,uf[110];
    26 arc e[10000];
    27 bool cmp(const arc &x,const arc &y){
    28     return x.w < y.w;
    29 }
    30 int Find(int x){
    31     if(x != uf[x]) uf[x] = Find(uf[x]);
    32     return uf[x];
    33 }
    34 int Kruskal(){
    35     for(int i = 0; i <= n; i++) uf[i] = i;
    36     sort(e,e+m,cmp);
    37     int ans = 0;
    38     for(int i = 0; i < m; i++){
    39         int x = Find(e[i].u);
    40         int y = Find(e[i].v);
    41         if(x != y){
    42             ans += e[i].w;
    43             uf[x] = y;
    44         }
    45     }
    46     return ans;
    47 }
    48 int main() {
    49     int u,v,w,k,i;
    50     while(scanf("%d",&n),n){
    51         m = n*(n-1)/2;
    52         for(i = 0; i < m; i++){
    53             scanf("%d %d %d %d",&u,&v,&w,&k);
    54             e[i] = arc(u,v,k?0:w);
    55         }
    56         printf("%d
    ",Kruskal());
    57     }
    58     return 0;
    59 }
    View Code
     
  • 相关阅读:
    C#中的int、long、float、double等类型都占多少个字节的内存
    Bit 存储操作代码碎片
    unity文件写入与读取
    unity调用系统剪切板功能
    LayerMask小结
    NGUI中获取鼠标在控件内部坐标
    【Unity技巧】Unity中的优化技术
    工程源码目录
    Unity3D_NGUI_性能优化实践_CPU卡顿
    Unity3d:UI面板管理整合进ToLua
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3941016.html
Copyright © 2011-2022 走看看