Bad Cowtractors
来源:互联网 发布:ubuntu 安装 分辨率 编辑:程序博客网 时间:2024/05/23 01:13
Description
Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ has already done some surveying, and found M (1 <= M <= 20,000) possible connection routes between pairs of barns. Each possible connection route has an associated cost C (1 <= C <= 100,000). Farmer John wants to spend the least amount on connecting the network; he doesn't even want to pay Bessie.
Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".
Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.
* Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.
Output
* Line 1: A single integer, containing the price of the most expensive tree connecting all the barns. If it is not possible to connect all the barns, output -1.
Sample Input
5 81 2 31 3 72 3 102 4 42 5 83 4 63 5 24 5 17
Sample Output
42
题解:克鲁斯卡尔不用考虑重边。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int INF = 0x3fffffff;struct Node{int from;int to;int cost;bool operator< (Node t) const{return cost > t.cost;}};Node e[200004];int pre[20000];int ans;int find(int x){return x == pre[x] ? x : pre[x] = find(pre[x]);}int max(int a,int b){return a > b ? a : b;}bool kruaskal(int n,int k){int j = 0;ans = 0;int cnt = 0;for(int i = 0;i < n;i++){for(;j < k;j++){int x = find(e[j].from);int y = find(e[j].to);if(x == y){continue;}++cnt;pre[x] = y;ans += e[j].cost;break;}}if(cnt != n - 1){return false;}return true;}int main(){int n,m;while(scanf("%d%d",&n,&m) != EOF){for(int i = 1;i <= n;i++){pre[i] = i;}int k = 0;for(int i = 0;i < m;i++){int u,v,c;scanf("%d%d%d",&u,&v,&c);e[k].from = u;e[k].to = v;e[k++].cost = c;e[k].from = v;e[k].to = u;e[k++].cost = c;}sort(e,e + k);if(kruaskal(n,k)) printf("%d\n",ans);else printf("-1\n");}return 0;}
普利姆(未优化):
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int INF = 0x3fffffff;int map[2003][2003];bool visited[2003];int d[2003];int ans;int max(int a,int b){return a > b ? a : b;}bool prim(int n){memset(visited,false,sizeof(visited));for(int i = 1;i <= n;i++){d[i] = map[1][i];}visited[1] = true;ans = 0;for(int i = 1;i < n;i++){int min = 0;int k;for(int j = 1;j <= n;j++){if(!visited[j] && min < d[j]){min = d[j];k = j;}}if(min == 0){return false;}ans += min;visited[k] = true;for(int j = 1;j <= n;j++){if(!visited[j] && d[j] < map[k][j]){d[j] = map[k][j];}}}return true;}int main(){int n,m;while(scanf("%d%d",&n,&m) != EOF){for(int i = 1;i <= n;i++){for(int j = 1;j <= n;j++){map[i][j] = 0;}}int u,v,c;for(int i = 0;i < m;i++){scanf("%d%d%d",&u,&v,&c);map[u][v] = map[v][u] = max(map[u][v],c);}if(prim(n)){printf("%d\n",ans);}else{printf("-1\n");}}return 0;}
0 0
- Bad Cowtractors
- Bad Cowtractors
- Bad Cowtractors
- Bad Cowtractors
- Bad Cowtractors
- Bad Cowtractors
- PKU 2377 Bad Cowtractors
- EOJ 5 Bad Cowtractors
- poj 2377 Bad Cowtractors
- poj 2377 Bad Cowtractors
- poj 2377 Bad Cowtractors
- poj2377 - Bad Cowtractors
- POJ_2377 Bad Cowtractors
- POJ 2377 Bad Cowtractors
- POJ 2377 Bad Cowtractors
- POJ-2377-Bad Cowtractors
- POJ 2377 Bad Cowtractors
- poj 2377 Bad Cowtractors
- bzoj2658: [Zjoi2012]小蓝的好友(mrx)
- hdu 1874 畅通工程续-spfa
- 第九章、通过表单和登录抓取
- Winsock简介
- python-var
- Bad Cowtractors
- The bytes/str dichotomy in Python 3
- 【基础数论】十分钟学会计算欧拉函数
- HTTP协议 (二) 基本认证
- A C I D
- cocos2dx 节点没有显示出来的可能原因
- 提升Table Views性能的10个建议
- Oracle COALESCE函数
- tomcat服务器使用