http://poj.org/problem?id=1287&&prim
来源:互联网 发布:55寸国产电视 知乎 编辑:程序博客网 时间:2024/06/10 06:08
Description
You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area.
Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.
Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.
Input
The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line.
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i.
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i.
Output
For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.
Sample Input
1 02 31 2 372 1 171 2 683 71 2 192 3 113 1 71 3 52 3 893 1 911 2 325 71 2 52 3 72 4 84 5 113 5 101 5 64 2 120
Sample Output
0171626
AC代码:
#include<iostream>#include<string.h>#include<algorithm>#include<limits.h>#include <stdlib.h>#define M 99999999#define N 51#define FOR(i,s,t) for(int i=(s);i<=t;++i)using namespace std;int map[N][N];bool visit[N];int dist[N];int n,m;void make_set(){ FOR(i,1,n) FOR(j,1,n) map[i][j]=M; cin>>m; FOR(i,1,m) { int a,b,c; cin>>a>>b>>c; if(map[b][a]>c) map[a][b]=map[b][a]=c; }}void prim(){ make_set(); FOR(i,1,n) {dist[i]=M;visit[i]=true;} int now=1; dist[now]=0; visit[now]=false; FOR(i,1,n) { FOR(j,1,n) if(visit[j]&&dist[j]>map[now][j]) dist[j]=map[now][j]; int minx=M; FOR(j,1,n) if(visit[j]&&dist[j]<minx) minx=dist[now=j]; visit[now]=false; } int ans=0; FOR(i,1,n) ans+=dist[i]; cout<<ans<<endl; } int main() { while(cin>>n&&n) prim(); return 0; }法二:
#include<iostream>#include<string.h>#include<algorithm>#include<limits.h>#define M 99999999#define N 3000#define FOR(i,s,t) for(int i=(s);i<=t;++i)using namespace std;struct edge{int x,y,len; }map[N];typedef struct edge Node;int father[51];int n,m;int cmp(const void *a,const void *b){return ((Node*)a)->len -((Node*)b)->len;}void make_set(){ FOR(i,1,n) father[i]=i; } void input() { cin>>m; FOR(i,1,m) cin>>map[i].x>>map[i].y>>map[i].len; qsort(map+1,m,sizeof(Node),cmp); } int find_set(int a) { while(a!=father[a]) a=father[a]; return a; } void Kusual() { make_set(); input(); int tot=0,ans=0; FOR(i,1,m) { int xx=find_set(map[i].x); int yy=find_set(map[i].y); if(xx!=yy) { father[yy]=xx; ans+=map[i].len; tot++; } if(tot==n-1) break; } cout<<ans<<endl; } int main() { while(cin>>n,n) Kusual(); system("pause"); }
- http://poj.org/problem?id=1287&&prim
- http://poj.org/problem?id=2965
- http://poj.org/problem?id=2075
- http://poj.org/problem?id=1125
- http://poj.org/problem?id=2983
- http://poj.org/problem?id=2406
- http://poj.org/problem?id=1961
- http://poj.org/problem?id=1486
- http://poj.org/problem?id=1062
- http://poj.org/problem?id=2983
- http://poj.org/problem?id=3159
- http://poj.org/problem?id=1201
- http://poj.org/problem?id=3259
- http://poj.org/problem?id=3233
- http://poj.org/problem?id=1273
- http://poj.org/problem?id=3281
- http://poj.org/problem?id=1007
- http://poj.org/problem?id=1840
- 终于明白了什么是.NET
- 桌面图标和文件夹图标全变成不可识别的、打不开的、不可读的
- 使用(EA)UML进行AS3程序设计
- jQuery入门:ready加载问题
- 使用天乐软件加密狗(JDProtect)保护您的软件,防止程序被跟踪/逆向/反编译/破解
- http://poj.org/problem?id=1287&&prim
- 谈谈我对数据结构
- ubuntu是linux家族中的祸害
- 数据结构线性表习题(二)
- 通过VC学习反汇编——函数调用:调用约定
- 如何获取客户端浏览器类型(PHP)
- 腾讯笔试附加题(C/C++)2011-10-10
- oreilly java swing : JTable 之 Table Data
- Windows Phone 监视内存使用情况