继续畅通工程

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
 
 #include <cstdio>
#include <queue>
#include <cstring> using namespace std; const int NV = ;
const int NE = ;
const int INF = <<;
int ne, nv, term, tot;
bool vis[NV];
int dist[NV];
struct Edge{
int v, cost, next;
Edge(){}
Edge(int a, int c){v = a, cost = c;}
Edge(int a, int c, int d){v = a, cost = c, next = d;}
bool operator < (const Edge& x) const
{
return cost > x.cost;
}
}edge[NE];
int eh[NV]; int prim(int s = )
{
for(int i = ; i <= nv; ++i) dist[i] = i == s ? : INF;
priority_queue<Edge> que;
que.push(Edge(s, ));
while(!que.empty())
{
Edge tmp = que.top();
int u = tmp.v;
int cost = tmp.cost;
que.pop();
if(vis[u]) continue;
vis[u] = true;
term += dist[u]; for(int i = eh[u]; i != -; i = edge[i].next)
{
int v = edge[i].v;
if(!vis[v] && dist[v] > edge[i].cost)
{
dist[v] = edge[i].cost;
que.push(Edge(v, dist[v]));
}
}
}
return term;
} void addedge(int u, int v, int cost)
{
Edge e = Edge(v, cost, eh[u]);
edge[tot] = e;
eh[u] = tot++;
return;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
while(scanf("%d", &nv) != EOF && nv)
{
memset(eh, -, sizeof(eh));
memset(vis, false, sizeof(vis));
term = tot = ;
int u, v, cost, state;
for(int i = ; i < nv * (nv - ) / ; ++i)
{
scanf("%d%d%d%d", &u, &v, &cost, &state);
if(state) cost = ;
addedge(u, v, cost);
addedge(v, u, cost);
}
prim();
printf("%d\n", term);
}
return ;
}
 #include<cstdio>
#include<cstring>
#include<algorithm>
#define SIZE 102
#define MAXN 10000 using namespace std; const int INF = <<;
int nv, ne;
struct Edge{
int u, v, cost;
}edge[MAXN]; int father[SIZE]; bool cmp(const struct Edge &a, const struct Edge &b)
{
return a.cost < b.cost;
} int find_father(int f)
{
return father[f] = f == father[f] ? f : find_father(father[f]);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
while(scanf("%d", &nv) != EOF && nv)
{
int state;
for(int i=; i<=nv; ++i) father[i] = i;
ne = nv*(nv-)/;
for(int i=; i<ne; ++i)
{
scanf("%d%d%d%d", &edge[i].u, &edge[i].v, &edge[i].cost, &state);
if(state)
{
int u = find_father(edge[i].u);
int v = find_father(edge[i].v);
father[u] = v;
}
}
int term = ;
sort(edge, edge+ne, cmp);
for(int i=; i<ne; ++i)
{ int u = find_father(edge[i].u);
int v = find_father(edge[i].v);
if(u != v)
{
term += edge[i].cost;
father[u] = v;
}
}
printf("%d\n", term);
}
return ;
}

HDU ACM 1879 继续畅通工程的更多相关文章

  1. hdu 1879 继续畅通工程 (并查集+最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1879 继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    ...

  2. hdu 1879 继续畅通工程

    /************************************************************************/ /* hdu 1879 继续畅通工程 Time L ...

  3. HDU 1879 继续畅通工程 (Prim(普里姆算法)+Kruskal(克鲁斯卡尔))

    继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  4. HDU 1879 继续畅通工程(最小生成树)

    省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经 ...

  5. hdu 1879 继续畅通工程 (最小生成树)

    继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  6. HDU 1879 继续畅通工程(Kruskra)

    继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  7. hdoj 1879 继续畅通工程

    继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  8. ACM题目————还是畅通工程

    Submit Status Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路 ...

  9. Hdoj 1879.继续畅通工程 题解

    Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计 ...

随机推荐

  1. Apache Commons Digester 三(规则注解)

    前言 Digester规则的定义除了可以在代码中直接new规则添加到 Digester对象外,还可以用xml配置规则,如下所示: <digester-rules> <pattern ...

  2. MySQL 一些内部原理

    1. MySQL 体系结构 如下图: Mysql是由SQL接口,解析器,优化器,缓存,存储引擎组成的(SQL Interface. Parser. Optimizer.Caches&Buffe ...

  3. 读vue-0.6-observer.js源码

    实现监听数组方法 var ArrayProxy = Object.create(Array.prototype), methods = ['push','pop','shift','unshift', ...

  4. Python 【web框架】之Flask

    flask 是Python实现的轻量级web框架.没有表单,orm等,但扩展性很好.很多Python web开发者十分喜欢.本篇介绍flask的简单使用及其扩展. 文中示例源码已经传到github:h ...

  5. React native 环境搭建遇到问题解决(android)

    新建项目 react-native init TestApp 运行项目 react-native run-android 不好意思,错误马上就到了 问题一 通常遇到这个错误之后,系统会给出这个具体详情 ...

  6. c# 对Url 解码编码

    /// <summary> /// 对Url进行编码 /// </summary> /// <param name="url">url</ ...

  7. PowerDesigner 修改Name ,Code 不变

    tools-> General   Options-> Dialog:Operation   Modes: 去掉 NameToCodeMirroring 前面的√      

  8. Hive原理总结(完整版)

    目录 课程大纲(HIVE增强) 3 1. Hive基本概念 4 1.1 Hive简介 4 1.1.1 什么是Hive 4 1.1.2 为什么使用Hive 4 1.1.3 Hive的特点 4 1.2 H ...

  9. java_分数

    题目内容: 设计一个表示分数的类Fraction.这个类用两个int类型的变量分别表示分子和分母. 这个类的构造函数是: Fraction(int a, int b) 构造一个a/b的分数. 这个类要 ...

  10. MarkDown常用格式

    常用格式 ** :加粗 <br> : 换行 > :可以用来引用文章,很漂亮. 可以展开的文件夹格式 <details> <summary>框架</sum ...