练习四 1005
来源:互联网 发布:相遇网络里 编辑:程序博客网 时间:2024/04/30 12:01
概述:省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全省畅通需要的最低成本。
思路:此题在1004题的基础上做了些许改动,即有些城镇之间早已有了路,把这个描述转化成代码的话很简单,只要让这条边的权值为0即可,这样就不会影响到数值了。代码也是在上题的技术上稍微改改就行了。
感想:怎么又是畅通工程啊。
#include <iostream>#include <fstream>#include <algorithm>using namespace std;const int N = 105;int father[N];int find(int x){if (x != father[x])father[x] = find(father[x]);return father[x];}struct edge{int x, y, v;}e[N*(N - 1) / 2];int cmp(edge e1, edge e2){return e1.v<e2.v;}int main(){ //ifstream cin("aaa.txt"); int n; while(cin>>n&&n) { int cnt=n; for (int i = 1; i <= n; ++i) father[i] = i; bool flag; for(int i=0;i<n*(n-1)/2;++i) { cin>>e[i].x>>e[i].y>>e[i].v>>flag; if(flag) e[i].v=0; } sort(e,e+n*(n-1)/2,cmp); int ans=0; for (int i = 0; i < n*(n-1)/2; ++i){int x = find(e[i].x);int y = find(e[i].y);if (x != y){ans += e[i].v;father[x] = y;--cnt;if(cnt ==1 ) break;}}cout << ans << endl; } return 0;}
0 0
- 练习四 1005
- 练习四 1005
- 练习四1005
- SDAU练习四 1005
- 练习四
- 练习四1005畅通工程2
- 数据库练习四
- 查询练习(四)
- Scala练习(四)
- 听课练习(四)
- 笔试练习四
- 笔试练习四
- 练习四1001
- 练习四 1001
- 练习四 1004
- 练习四 1003
- 练习四 1002
- 练习四 1001
- 练习四 1002
- Status bar
- hive搭建
- JMeter简介
- How to Fix GNOME License Not Accepted Issue on CentOS 7
- 练习四 1005
- 1011
- TCP/IP七层网络结构
- 商城系统中常见的逻辑陷阱和优化方案(1)
- web Deploy网站发布
- CC2530在IAR下printf重定向
- 作用域指针 boost::scoped_ptr
- HDU1113 POJ1318 UVA642 ZOJ1181 Word Amalgamation
- Java关键字volatile修饰变量