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
当N为0时输入结束。
#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 继续畅通工程的更多相关文章
- hdu 1879 继续畅通工程 (并查集+最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1879 继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1879 继续畅通工程
/************************************************************************/ /* hdu 1879 继续畅通工程 Time L ...
- HDU 1879 继续畅通工程 (Prim(普里姆算法)+Kruskal(克鲁斯卡尔))
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HDU 1879 继续畅通工程(最小生成树)
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经 ...
- hdu 1879 继续畅通工程 (最小生成树)
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HDU 1879 继续畅通工程(Kruskra)
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- hdoj 1879 继续畅通工程
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- ACM题目————还是畅通工程
Submit Status Description 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路 ...
- Hdoj 1879.继续畅通工程 题解
Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计 ...
随机推荐
- 对话Task
上一篇简单讲解了 线程和线程池以及上下文切换.创建线程代价高昂,默认每个线程都要占用大量虚拟内存1M.更有效的做法使用线程池,重复利用线程.在.NET4.0中引入了TPL任务并行库,你可以在将精力集中 ...
- 删除.svn 脱离svn版本控制器
1.for /r . %%a in (.) do @if exist "%%a\.svn" rd /s /q "%%a\.svn" 复制到记事本,将记事本保存为 ...
- zookeeper集群操作【这里只说明简单的操作步骤,zk的相关参数、说明请参考官方文档】
本文版权归 远方的风lyh和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. [这里是在一台机器上搭建的 zk伪集群] 1.从官网下载下载zk http://apa ...
- 利用jquery操作隐藏table某一列
本文版权归 远方的风lyh和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. //隐藏表格第一列 $('tr').find('th:eq(0)').hide(); $('tr').f ...
- iOS开发(0):框架QMUIKit的使用 | 使用第三方UI框架 | cocoapods的使用
对于移动APP来说,客户端(iOS或android)的界面开发是必不可少的工作.为了减轻界面开发的工作量,也为了提高开发的速度,选择一个良好的界面框架,是有意义的. iOS开源的界面框架有很多,比如c ...
- PHP常用的正则表达式(有些需要调整)
平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用: "^\d+$" //非负整数(正整数 + 0) 顺平注: 验证输入id数值,不能为0 $reg1='/ ...
- Django 学习笔记(三) --- HTML 模版加载 css、js、img 静态文件
人生苦短 ~ Tips:仅适用于 Python 3+(反正差别不大,py2 改改也能用).因为据 Python 之父 Guido van Rossum 说会在 2020 年停止对 Python 2 的 ...
- SpringMVC+SpringJdbc+SQLServer+EasyUI增删改查
前言 前天用SpringJdbc连接了一把SQLServer,早上起来用SpringMVC+SpringJdbc+EasUI写了个增删改查的demo,主要是熟悉下SpringMVC相关知识点,如vie ...
- [转]Angular4---部署---将Angular项目部署到IIS上
本文转自:https://www.cnblogs.com/kingkangstudy/p/7699710.html Angular项目部署到一个IIS服务器上 1.安装URL rewrite组件: 网 ...
- Pseudocode MD5 CODE
//Note: All variables are unsigned 32 bit and wrap modulo 2^32 when calculating var int[64] s, K //s ...