poj

Jungle Roads

The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.

Input

The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.
Output

The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.
Sample Input

9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0
Sample Output

216
30
Source

Mid-Central USA 2002

算法:map+最小生成树

代码:

#include <iostream>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <string>
#include <iomanip>
#include <map>
#define inf 100000000
using namespace std;
map<string,int>dp;
struct dot
{
	int x,y,v;
} node[100000];
int pre[30],dis[30][30];
int cmp(dot n1,dot n2)
{return n1.v<n2.v;}
int find (int ax)
{
	while(ax!=pre[ax]) ax=pre[ax];
	return ax;
}
void init()
{
	for(int i=1;i<=27;i++)
	{
		for(int j=1;j<=27;j++)
		dis[i][j]=inf;
	}
}
int main()
{
	int n,m,i,j,k,kk,step;
	string s,t;
	char ch;
	while(cin>>n&&n)
	{
		k=1;
		init();
		for(i=1;i<n;i++)
		{
			cin>>s;
			if(!dp[s])
			dp[s]=k++;
			cin>>m;
			while(m--)
			{
				cin>>t>>step;
				if(!dp[t]) dp[t]=k++;
				dis[dp[s]][dp[t]]=min(dis[dp[t]][dp[s]],step);
				dis[dp[t]][dp[s]]=min(dis[dp[t]][dp[s]],step);
			}
		}
		kk=0;
		for(i=1;i<=n;i++)
		{
			pre[i]=i;
			for(j=1;j<=n;j++)
			{
				if(dis[i][j]!=inf)
				{
					node[kk].x=i;
					node[kk].y=j;
					node[kk++].v=dis[i][j];
				}
			}
		}
		sort(node,node+kk,cmp);
		int sum=0;
		for(i=0;i<kk;i++)
		{
			int fx=find(node[i].x);
			int fy=find(node[i].y);
			if(fx!=fy)
			{
				sum+=node[i].v;
				pre[fx]=fy;
			}
		}
		dp.clear();
		cout<<sum<<endl; 
	}
	return 0;
}
Agri-Net

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.
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

USACO 102

算法:简单的最小生成树

代码:

#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>
#include <algorithm>
using namespace std;
struct dot
{
	int x,y,v;
}node[2000005];
int pre[105];
int map[105][105];
int cmp(dot n1,dot n2)
{return n1.v<n2.v;}
int find(int ax)
{
	while(ax!=pre[ax]) ax=pre[ax];
	return ax;
}
int main()
{
	int i,j,k,n,m;
	while(cin>>n&&n)
	{
		k=0;
		for(i=1;i<=n;i++)
		{
			pre[i]=i;
			for(j=1;j<=n;j++)
			{
				cin>>map[i][j];
				if(i>j)
				{
					node[k].x=i;
					node[k].y=j;
					node[k++].v=map[i][j];
				}
			}
		}
		sort(node,node+k,cmp);
		int sum=0;
		for(i=0;i<k;i++)
		{
			int fx=find(node[i].x);
			int fy=find(node[i].y);
			if(fx!=fy)
			{
				sum+=node[i].v;
				pre[fy]=fx;
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}

HighwaysDescription

The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system.

Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.

The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.
Input

The first line of input is an integer T, which tells how many test cases followed.
The first line of each case is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. There is an empty line after each test case.
Output

For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.
Sample Input

1

3
0 990 692
990 0 179
692 179 0
Sample Output

692
Hint

Huge input,scanf is recommended.
Source

POJ Contest,Author:Mathematica@ZSU

算法:最小生成树;

分析:输出生成树上边的权值最大值;

代码:

#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>
#include <algorithm>
using namespace std;
struct dot
{
	int x,y,v;
}node[3000005];
int pre[505],map[505][505];
int cmp(dot n1,dot n2)
{return n1.v<n2.v;}
int find(int ax)
{
	int i=ax,j;
	while(ax!=pre[ax])ax=pre[ax];
	return ax;
}
int main()
{
	int i,j,k,m,n,T;
	cin>>T;
	while(T--)
	{
		cin>>n;
		k=0;
		for(i=1;i<=n;i++)
		{
			pre[i]=i;
			for(j=1;j<=n;j++)
			{
				cin>>map[i][j];
				if(i>j)
				{
					node[k].x=i;
					node[k].y=j;
					node[k++].v=map[i][j];
				}
			}
		}
		sort(node,node+k,cmp);
		int sum=0;
		for(i=0;i<k;i++)
		{
			int fx=find(node[i].x);
			int fy=find(node[i].y);
			if(fx!=fy)
			{
				if(sum<node[i].v) sum=node[i].v;
				pre[fy]=fx;
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}

Truck History

Description

Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company has its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (each letter on each position has a very special meaning but that is unimportant for this task). At the beginning of company's history, just a single truck type was used but later other types were derived from it, then from the new types another types were derived, and so on.

Today, ACM is rich enough to pay historians to study its history. One thing historians tried to find out is so called derivation plan -- i.e. how the truck types were derived. They defined the distance of truck types as the number of positions with different letters in truck type codes. They also assumed that each truck type was derived from exactly one other truck type (except for the first truck type which was not derived from any other type). The quality of a derivation plan was then defined as
1/Σ(to,td)d(to,td)

where the sum goes over all pairs of types in the derivation plan such that to is the original type and td the type derived from it and d(to,td) is the distance of the types.
Since historians failed, you are to write a program to help them. Given the codes of truck types, your program should find the highest possible quality of a derivation plan.
Input

The input consists of several test cases. Each test case begins with a line containing the number of truck types, N, 2 <= N <= 2 000. Each of the following N lines of input contains one truck type code (a string of seven lowercase letters). You may assume that the codes uniquely describe the trucks, i.e., no two of these N lines are the same. The input is terminated with zero at the place of number of truck types.
Output

For each test case, your program should output the text "The highest possible quality is 1/Q.", where 1/Q is the quality of the best derivation plan.
Sample Input

4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0
Sample Output

The highest possible quality is 1/3.
Source

CTU Open 2003

代码:

#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>
#include <algorithm>
#include <stdio.h>
using namespace std;
int map[2005][2005];
int pre[2005];
struct dot
{
	int x,y,v;
} node[40000005];
int cmp(dot n1,dot n2)
{return n1.v<n2.v;}
int find(int ax)
{
	while(ax!=pre[ax])ax=pre[ax];
	return ax;
}
int main()
{
	int n,m,i,j,k,p,q;
	string st[2005]; 
	while(cin>>n&&n)
	{
		memset(map,0,sizeof(map));
		for(i=0;i<n;i++)
		{
			cin>>st[i];
			pre[i]=i;
		}
		p=0;
		for(i=0;i<n-1;i++)
		{
			for(j=i+1;j<n;j++)
			{
				for(k=0;k<7;k++)
				{
					if(st[i][k]!=st[j][k])
					map[i][j]++;
				}
				node[p].x=i;
				node[p].y=j;
				node[p++].v=map[i][j];
			}
		}
		sort(node,node+p,cmp);
		int sum=0;
		k=0;
		for(i=0;i<p;i++)
		{
			int fx=find(node[i].x);
			int fy=find(node[i].y);
			if(fx!=fy)
			{
				k++;
				pre[fy]=fx;
				sum+=node[i].v; 
			}
			if(k==n-1) break;
		}
		cout<<"The highest possible quality is 1/"<<sum<<"."<<endl;
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值