http://poj.org/problem?id=1258
Agri-Net
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 28118 | Accepted: 11148 |
Description
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Input
The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.
Output
For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
Sample Input
4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0
Sample Output
28
Source
题意:这题题意好理解,就是让村子里的所有家庭的宽带都连接,求最短的光钎。很明显的求最小生成树的问题。还是套用2458,1789的模版。一点都没有变(这题以前居然做过。。)
以前代码1:
#include<stdio.h>
#include<string.h>
#define Max 1000000
#define N 1000
int map[N][N],low[N],visited[N];//map[][]保存输入的数据,visited[][]表示有没有访问过;low[]保存最小的权值
int n;
int prime()
{
int i,j,pos,min,result=0;
memset(visited,0,sizeof(visited));//初始化数据使所有的数都未访问过(0:表示)
visited[1]=1;//从第一个数开始访问(可以是任意的数)
pos=1;
for(i=1;i<=n;i++)//先从第一行中找出一个最小值
{
if(i!=pos)
low[i]=map[pos][i];
}
for(i=1;i<n;i++)//树中n个顶点共生成n-1条边
{
min=Max;
for(j=1;j<=n;j++)
{
if(visited[j]==0&&low[j]<min)
{
min=low[j];
pos=j;
}
}
result+=min;
visited[pos]=1;
for(j=1;j<=n;j++)
{
if(visited[j]==0&&low[j]>map[pos][j])
low[j]=map[pos][j];
}
}
return result;
}
int main()
{
int i,j,ans;
while(scanf("%d",&n)!=EOF)
{
memset(map,Max,sizeof(map));//初始化为无穷大;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&map[i][j]);//输入数据
}
}
ans=prime();
printf("%d\n",ans);
}
return 0;
}
#include<string.h>
#define Max 1000000
#define N 1000
int map[N][N],low[N],visited[N];//map[][]保存输入的数据,visited[][]表示有没有访问过;low[]保存最小的权值
int n;
int prime()
{
}
int main()
{
}
现在代码
#include<stdio.h>
#include<string.h>
#define MAXN 1010
int map[MAXN][MAXN],lowcost[MAXN],closest[MAXN],cost[MAXN][MAXN],maxn;
int n;
#define INF 0x3f3f3f3f
int prime()
{
maxn =0;
int i,j,k,min;
int ans=0;
for(i=0;i<n;i++)
{
lowcost[i]=cost[0][i];
closest[i]=0;
}
for(i=0;i<n-1;i++)
{
min=INF;
for(j=0;j<n;j++)
{
if(lowcost[j]&&min>lowcost[j])
{
min=lowcost[j];
k=j;
}
}
ans+=lowcost[k];
lowcost[k]=0;
for(j=0;j<n;j++)
{
if(cost[k][j]<lowcost[j])
{
lowcost[j]=cost[k][j];
}
}
}
return ans;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&cost[i][j]);
}
printf("%d\n",prime());
}
return 0;
}
504K | 16MS | C++ | 1755B |
#include<stdio.h>
#include<string.h>
#define MAXN 1010
int map[MAXN][MAXN],lowcost[MAXN],closest[MAXN],cost[MAXN][MAXN],maxn;
int n;
#define INF 0x3f3f3f3f
int prime()
{
}
int main()
{
}
2012.9.5//又是在poj1789和2485的基础上直接套prim()。。。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 110
char s[MAXN][10];
int map[MAXN][10];
int vis[MAXN];
int lowcost[MAXN],cost[MAXN][MAXN];
int n;
#define inf 0x3f3f3f3f
int prim()
{
int max=-10000;
memset(vis,0,sizeof(vis));
int ans=0;
for(int i=0;i<n;i++)
{
lowcost[i]=cost[0][i];
}
vis[0]=1;
int min;
int k;
for(int i=0;i<n-1;i++)
{
min=inf;
for(int j=0;j<n;j++)
{
if(!vis[j]&&lowcost[j]<min)
{
min=lowcost[j];
k=j;
}
}
//max=max>lowcost[k]?max:lowcost[k];
ans+=lowcost[k];
vis[k]=1;
for(int j=0;j<n;j++)
{
if(cost[j][k]<lowcost[j]&&vis[j]==0)
{
lowcost[j]=cost[j][k];
}
}
}
//return max;
return ans;
}
int main()
{
int t;
while(scanf("%d",&n)!=EOF)
{
int i,j;
memset(cost,0,sizeof(cost));
//scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&cost[i][j]);
}
}
printf("%d\n",prim());
}
return 0;
}
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 110
char s[MAXN][10];
int map[MAXN][10];
int vis[MAXN];
int lowcost[MAXN],cost[MAXN][MAXN];
int n;
#define inf 0x3f3f3f3f
int prim()
{
}
int main()
{
}