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 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计 ...
随机推荐
- session失效时间设置
session失效时间设置 一.java代码 request.getSession().setMaxInactiveInterval(1800);/*秒为单位*/ 二.web.xml <sess ...
- hashmap可以用null为键值
import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class TestMain { ...
- 课程四(Convolutional Neural Networks),第一周(Foundations of Convolutional Neural Networks) —— 3.Programming assignments:Convolutional Model: application
Convolutional Neural Networks: Application Welcome to Course 4's second assignment! In this notebook ...
- 监督学习Supervised Learning
In supervised learning, we are given a data set and already know what our correct output should look ...
- 【awesome-dotnet-core-learning】(2)-Sprache.Calc-表达式计算器
[awesome-dotnet-core-learning](2)-Sprache.Calc-表达式计算器 示例代码托管在GITHUB上, 欢迎STAR! 严格来说, Sprache.Calc只是上一 ...
- API 接口返回值
API 接口返回值 https://blog.csdn.net/baple/article/details/52925772
- NABCD校园生活
N(Need,需求) 在校师生得到的信息分散,极大的影响了师生的学习效率,所以就会有快速得到全面信息的需求,而我们的APP正是在解决这个问题. A(Approach,做法) 我们会在主页进行信息分类, ...
- LeetCode算法笔记目录
贪心算法: LeetCode翻转矩阵后的得分-Python3<六> LeetCode根据身高重建队列-Python3<七> LeetCode 任务调度器-Python3< ...
- LeetCode翻转矩阵后的得分-Python3<六>
上一篇:LeetCode子域名访问计数-Python3.7<五> 题目:https://leetcode-cn.com/problems/score-after-flipping-matr ...
- CMD下进入MYSQL的命令
CMD下进入MYSQL的命令 mysql -h localhost -u root -p 切到MYSQL的bin目录下,输入上面命令,回车 然后输入密码 回车