继续畅通工程

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. ES6中的proxy

    1 概述 Proxy 可以理解成,在目标对象之前架设一层“拦截”,外界对该对象的访问,都必须先通过这层拦截,因此提供了一种机制,可以对外界的访问进行过滤和改写. Proxy 这个词的原意是代理,用在这 ...

  2. Jenkins系列之二——centos 6.9 + JenKins 安装

    centos 6.9 + JenKins 安装记录环境: [root@localhost ~]# cat /etc/issue CentOS release 6.9 (Final) Kernel \r ...

  3. mac下nginx搭建

    首先使用brew安装nginx brew install nginx 安装完毕后,如果我们要使用nginx监听本地的80端口,需要改掉mac自带的apache占用的80端口 sudo vim /etc ...

  4. interface21 - web - Log4jConfigListener(Log4j加载流程)

    前言 最近打算花点时间好好看看spring的源码,然而现在Spring的源码经过迭代的版本太多了,比较庞大,看起来比较累,所以准备从最初的版本(interface21)开始入手,仅用于学习,理解其设计 ...

  5. Python中property的使用

  6. redis 集群搭建: redis-cluster

    前言 redis数据存储在内存中, 就会受到内存的限制, 大家都知道, 一台电脑, 硬盘可以有1T, 但是内存, 没有听说有1T的内存吧. 那如果数据非常多, 超过一台电脑的内存空间, 怎么办呢? 正 ...

  7. java使用java.lang.management监视和管理 Java 虚拟机

    ClassLoadingMXBean 用于 Java 虚拟机的类加载系统的管理接口. CompilationMXBean 用于 Java 虚拟机的编译系统的管理接口. GarbageCollector ...

  8. javascript的作用域和优先级

    变量的作用域是在定义时决定的,不是在运行时活动对象是在运行时决定的?如果就创建一个对象,使用完毕就完了,就使用json字面量的方式如果对象被反复创建,反复使用,就使用自定义的构造函数方式优先级内部变量 ...

  9. Linux 服务器命令,持续更新……

    记录一下常用命令给自己备忘备查,会持续更新-- 一.查看和修改Linux的时间 1. 查看时间和日期,命令: date 2.设定时间和日期 例如:将系统日期修改成2020年2月14日12点的命令: d ...

  10. 深入浅出 JVM GC(3)

    # 前言 在 深入浅出 JVM GC(2) 中,我们介绍了一些 GC 算法,GC 名词,同时也留下了一个问题,就是每个 GC 收集器的具体作用.有哪些 GC 收集器呢? Serial 串行收集器(只适 ...