• HDU ACM 1879 继续畅通工程


    继续畅通工程

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 10206    Accepted Submission(s): 4451


    Problem Description
    省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全省畅通需要的最低成本。
     
    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
     
    Author
    ZJU
     
    Source
     
     1 #include <cstdio>
     2 #include <queue>
     3 #include <cstring>
     4 
     5 using namespace std;
     6 
     7 const int NV = 102;
     8 const int NE = 10000;
     9 const int INF = 1<<30;
    10 int ne, nv, term, tot;
    11 bool vis[NV];
    12 int dist[NV];
    13 struct Edge{
    14     int v, cost, next;
    15     Edge(){}
    16     Edge(int a, int c){v = a, cost = c;}
    17     Edge(int a, int c, int d){v = a, cost = c, next = d;}
    18     bool operator < (const Edge& x) const
    19     {
    20         return cost > x.cost; 
    21     }
    22 }edge[NE];
    23 int eh[NV];
    24 
    25 int prim(int s = 1)
    26 {
    27     for(int i = 1; i <= nv; ++i) dist[i] = i == s ? 0 : INF;
    28     priority_queue<Edge> que;
    29     que.push(Edge(s, 0));
    30     while(!que.empty())
    31     {
    32         Edge tmp = que.top();
    33         int u = tmp.v;
    34         int cost = tmp.cost;
    35         que.pop();
    36         if(vis[u]) continue;
    37         vis[u] = true;
    38         term += dist[u];
    39         
    40         for(int i = eh[u]; i != -1; i = edge[i].next)
    41         {
    42             int v = edge[i].v;
    43             if(!vis[v] && dist[v] > edge[i].cost)
    44             {
    45                 dist[v] = edge[i].cost;
    46                 que.push(Edge(v, dist[v]));
    47             }
    48         }
    49     }
    50     return term;
    51 }
    52 
    53 void addedge(int u, int v, int cost)
    54 {
    55     Edge e = Edge(v, cost, eh[u]);
    56     edge[tot] = e;
    57     eh[u] = tot++;
    58     return;
    59 }
    60 
    61 int main()
    62 {
    63     #ifndef ONLINE_JUDGE
    64     freopen("input.txt", "r", stdin);
    65     #endif
    66     while(scanf("%d", &nv) != EOF && nv)
    67     {
    68         memset(eh, -1, sizeof(eh));
    69         memset(vis, false, sizeof(vis));
    70         term = tot = 0;
    71         int u, v, cost, state;
    72         for(int i = 0; i < nv * (nv - 1) / 2; ++i)
    73         {
    74             scanf("%d%d%d%d", &u, &v, &cost, &state);
    75             if(state) cost = 0;
    76             addedge(u, v, cost);
    77             addedge(v, u, cost);
    78         }
    79         prim();
    80         printf("%d
    ", term);
    81     }
    82     return 0;
    83 }
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #define SIZE 102
     5 #define MAXN 10000
     6 
     7 using namespace std;
     8 
     9 const int INF = 1<<30;
    10 int nv, ne;
    11 struct Edge{
    12     int u, v, cost;
    13 }edge[MAXN];
    14 
    15 int father[SIZE];
    16 
    17 bool cmp(const struct Edge &a, const struct Edge &b)
    18 {
    19     return a.cost < b.cost;
    20 }
    21 
    22 int find_father(int f)
    23 {
    24     return father[f] = f == father[f] ? f : find_father(father[f]);
    25 }
    26 
    27 int main()
    28 {
    29     #ifndef ONLINE_JUDGE
    30     freopen("input.txt", "r", stdin);
    31     #endif
    32     while(scanf("%d", &nv) != EOF && nv)
    33     {
    34         int state;
    35         for(int i=1; i<=nv; ++i) father[i] = i;
    36         ne = nv*(nv-1)/2;
    37         for(int i=0; i<ne; ++i)
    38         {
    39             scanf("%d%d%d%d", &edge[i].u, &edge[i].v, &edge[i].cost, &state);
    40             if(state)
    41             {
    42                 int u = find_father(edge[i].u);
    43                 int v = find_father(edge[i].v);
    44                 father[u] = v;
    45             }
    46         }
    47         int term = 0;
    48         sort(edge, edge+ne, cmp);
    49         for(int i=0; i<ne; ++i)
    50         {
    51             
    52             int u = find_father(edge[i].u);
    53             int v = find_father(edge[i].v);
    54             if(u != v) 
    55             {
    56                 term += edge[i].cost;
    57                 father[u] = v;
    58             }
    59         }
    60         printf("%d
    ", term);
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    tf2 callback
    JAVA 8 新特性 Optional类
    JAVA 8 新特性 Stream API 终止操作
    JAVA 8 新特性 Stream API 中间操作
    牙醫分普通科8類專科
    asterisk todo
    室内空气质量鉴定
    javascript, jquery, nodejs学习2
    firefox extension教程
    Bjarne Stroustrup announces C++ Core Guidelines
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/3223347.html
Copyright © 2020-2023  润新知