Networking 【poj-1287】 【最小生成树】
来源:互联网 发布:魔兽争霸3 for mac 编辑:程序博客网 时间:2024/06/05 05:48
Networking
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 11900 Accepted: 6424
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
题意:给出n个节点,再有m条边,这m条边代表从a节点到b节点电缆的长度,现在要你将所有节点都连起来,并且使长度最小。
题解:最小生成树,不过输入有重边,要特别注意。两种算法,用Kruskal算法比较简单;用Prim算法需要考虑重边的情况。
代码如下:
《1》Kruskal
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int maxn=105;int n,m;int par[maxn];int Rank[maxn];int ans;struct Edge{ int x; int y; int v;}e[maxn*maxn/2];void init(){ for(int i=0;i<=n;i++){ par[i]=i; Rank[i]=0; }}bool cmp(Edge e1,Edge e2){ return e1.v<e2.v;}int find(int p){ if(p!=par[p]) par[p]=find(par[p]); return par[p];}void unite(int x,int y){ if(Rank[x]>Rank[y]) par[y]=x; else{ if(Rank[x]==Rank[y]) Rank[y]++; par[x] = y; } } void Kruskal(){ init(); ans=0; sort(e,e+m,cmp); for(int i=0;i<m;i++){ int x=find(e[i].x); int y=find(e[i].y); if(x!=y){ ans+=e[i].v; unite(x,y); } } }int main(){ while(scanf("%d",&n)&&n){ scanf("%d",&m); for(int i=0;i<m;i++){ scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].v); } Kruskal(); printf("%d\n",ans); } return 0;}
《2》Prim
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int maxn=105;const int INF=0x3f3f3f3f;int n,m;int cost[maxn][maxn];int vis[maxn][maxn];int mincost[maxn];bool used[maxn];int prim(){for(int i=1;i<=n;i++){mincost[i]=INF;used[i]=false;}mincost[1]=0;int res=0;while(true){int v=-1;for(int u=1;u<=n;u++){if(!used[u]&&(v==-1||mincost[u]<mincost[v]))v=u;}if(v==-1) break;used[v]=true;res+=mincost[v];for(int u=1;u<=n;u++){mincost[u]=min(mincost[u],cost[v][u]);}}return res;}int main(){while(scanf("%d",&n)&&n){scanf("%d",&m);int a,b,c;memset(vis,0,sizeof(vis));for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){cost[i][j]=INF;}}for(int i=0;i<m;i++){scanf("%d%d%d",&a,&b,&c);if(vis[a][b]==1&&cost[a][b]>c){cost[a][b]=c;vis[a][b]=1;}else if(vis[a][b]==0){cost[a][b]=c;vis[a][b]=1;}}for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){cost[i][j]=cost[j][i]=min(cost[i][j],cost[j][i]);}}int sum=prim();printf("%d\n",sum);}return 0;}
阅读全文
0 0
- POJ 1287 最小生成树 Networking
- poj 1287 Networking 最小生成树
- POJ 1287 Networking(最小生成树)
- poj 1287 Networking(图论:最小生成树)
- poj-1287 Networking 最小生成树
- POJ 1287 Networking(最小生成树)
- POJ 1287 Networking(最小生成树)
- poj 1287 Networking(最小生成树)
- poj 1287 Networking ->最小生成树
- Poj 1287 Networking【最小生成树】
- POJ - 1287 Networking(最小生成树)
- poj 1287 Networking(最小生成树)
- POJ 1287 Networking(最小生成树)
- poj 1287 Networking (最小生成树)
- POJ 1287 Networking(最小生成树)
- 【POJ】-1287-Networking(最小生成树)
- poj-1287-Networking【最小生成树】
- POJ 1287 Networking (最小生成树)
- python3使用flask框架搭建在线词云应用
- 洛谷3600,大力期望DP
- STL容器 -map和set接口介绍
- [Unity插件]DOTween基础
- 单片机的启动过程(从上电到main)
- Networking 【poj-1287】 【最小生成树】
- 事件案例体验
- 达内课程-面向对象之接口
- JavaScript :几个小练习
- Apache服务器
- js定时器(二)延时消失的菜单
- C语言学习重点总结(一)基本数据类型分析
- 实现两个工程之间跳转
- Array数组声明初始化的一些写法