继续畅通工程

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. SpringBoot之OAuth2.0学习之客户端快速上手

    2.1.@EnableOAuth2Sso 这个注解是为了开启OAuth2.0的sso功能,如果我们配置了WebSecurityConfigurerAdapter,它通过添加身份验证过滤器和身份验证(e ...

  2. rfc2818 --- HTTP Over TLS

    协议链接 本协议描述了如何使用TLS来对Internet上的HTTP进行安全加固. 2.1. Connection Initiation(链接初始化) HTTP client同时也作为TLS clie ...

  3. ruby执行字符串代码

    str = "a='abcd'; a.reverse" 字符串str为ruby代码,执行方法eval eval str => "dcba"

  4. bootstrap3中container与container_fluid的区别

    .container与.container_fluid是bootstrap中的两种不同类型的外层容器,按照官方的说法,这两者的区别是: .container 类用于固定宽度并支持响应式布局的容器. . ...

  5. Shell 实例:备份最后一天内所有修改过的文件

    在一个"tarball"中(经过 tar 和 gzip 处理过的文件)备份最后 24 小时之内当前目录下所有修改的文件. 程序代码如下: #!/bin/bash BACKUPFIL ...

  6. Maven配置国内镜像仓库

    eclipse 位置

  7. 微信公众号开发--.Net Core实现微信消息加解密

    1:准备工作 进入微信公众号后台设置微信服务器配置参数(注意:Token和EncodingAESKey必须和微信服务器验证参数保持一致,不然验证不会通过). 2:基本配置 设置为安全模式 3.代码实现 ...

  8. UML速记

    依赖:虚线箭头 关联:实线箭头 接口:虚线三角 父类:实线三角 聚合:空心菱形 组合:实心菱形 顺着箭头方向: 依赖于和什么关联是什么的子类是什么的接口的实现是什么的聚合是什么的组合

  9. [android] 安卓消息推送的几种实现方式

    消息推送的目的:让服务器端及时的通知客户端 实现方案 轮询:客户端每隔一定的时间向服务器端发起请求,获得最新的消息 特点:如果用在最新新闻通知上,效率就有点低了,技术简单,好实现 应用场景:服务器端以 ...

  10. IDEA新建javaWeb项目

    创建JavaWeb项目的步骤大致如下: 创建JavaWeb项目之前所需要的条件 - 安装jdk - 安装服务器(如:tomcat) - 安装idea 新建项目 New-->Project...