poj 1751 Highways ( 最小生成树--prim算法 )
2016-08-05 08:35
267 查看
Highways
Description
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the most important towns.
However, there are still some towns that you can't reach via a highway. It is necessary to build more 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 and town i has a position given by the Cartesian coordinates (xi, yi). Each highway connects exaclty two towns. All highways (both the original ones and the ones that are to be built) follow straight lines, and thus
their length is equal to Cartesian distance between towns. 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 cost of building new highways. However, they want to guarantee that every town is highway-reachable from every other town. Since Flatopia is so flat, the cost of a highway is always proportional to its length.
Thus, the least expensive highway system will be the one that minimizes the total highways length.
Input
The input consists of two parts. The first part describes all towns in the country, and the second part describes all of the highways that have already been built.
The first line of the input file contains a single integer N (1 <= N <= 750), representing the number of towns. The next N lines each contain two integers, xi and yi separated by a space. These values give the coordinates of ith town (for i from
1 to N). Coordinates will have an absolute value no greater than 10000. Every town has a unique location.
The next line contains a single integer M (0 <= M <= 1000), representing the number of existing highways. The next M lines each contain a pair of integers separated by a space. These two integers give a pair of town numbers which are already connected by a
highway. Each pair of towns is connected by at most one highway.
Output
Write to the output a single line for each new highway that should be built in order to connect all towns with minimal possible total length of new highways. Each highway should be presented by printing town numbers that this highway connects, separated by
a space.
If no new highways need to be built (all towns are already connected), then the output file should be created but it should be empty.
Sample Input
Sample Output
prim:用了一个技巧,把已经联通的路距离构图时距离记为 0 ,
代码:
#include<cstdio>
#include<cstring>
#define MAXN 800
#define INF 0x3f3f3f
#include<algorithm>
using namespace std;
int dis[MAXN];
bool vis[MAXN];
int map[MAXN][MAXN];
int mark[MAXN];
void prim(int n)
{
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[1]=0;
for(int i=1;i<=n;i++)
{
int k=-1;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&(k==-1||dis[j]<dis[k]))
k=j;
}
vis[k]=1;
if(dis[k]!=INF&&dis[k]!=0)
printf("%d %d\n",mark[k],k);
for(int j=1;j<=n;j++)
{
if(!vis[j]&&dis[j]>map[k][j])
{
dis[j]=map[k][j];
mark[j]=k;
}
}
}
}
int main()
{
int n,m,a,b,x[MAXN],y[MAXN];
while(~scanf("%d",&n))
{
memset(map,INF,sizeof(map));
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x[i],&y[i]);
}
for(int i=1;i<n;i++)
{
for(int j=i;j<=n;j++)
{
map[i][j]=map[j][i]=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
}
}
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
map[a][b]=map[b][a]=0; //已经有通路
}
prim(n);
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 13293 | Accepted: 3843 | Special Judge |
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the most important towns.
However, there are still some towns that you can't reach via a highway. It is necessary to build more 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 and town i has a position given by the Cartesian coordinates (xi, yi). Each highway connects exaclty two towns. All highways (both the original ones and the ones that are to be built) follow straight lines, and thus
their length is equal to Cartesian distance between towns. 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 cost of building new highways. However, they want to guarantee that every town is highway-reachable from every other town. Since Flatopia is so flat, the cost of a highway is always proportional to its length.
Thus, the least expensive highway system will be the one that minimizes the total highways length.
Input
The input consists of two parts. The first part describes all towns in the country, and the second part describes all of the highways that have already been built.
The first line of the input file contains a single integer N (1 <= N <= 750), representing the number of towns. The next N lines each contain two integers, xi and yi separated by a space. These values give the coordinates of ith town (for i from
1 to N). Coordinates will have an absolute value no greater than 10000. Every town has a unique location.
The next line contains a single integer M (0 <= M <= 1000), representing the number of existing highways. The next M lines each contain a pair of integers separated by a space. These two integers give a pair of town numbers which are already connected by a
highway. Each pair of towns is connected by at most one highway.
Output
Write to the output a single line for each new highway that should be built in order to connect all towns with minimal possible total length of new highways. Each highway should be presented by printing town numbers that this highway connects, separated by
a space.
If no new highways need to be built (all towns are already connected), then the output file should be created but it should be empty.
Sample Input
9 1 5 0 0 3 2 4 5 5 1 0 4 5 2 1 2 5 3 3 1 3 9 7 1 2
Sample Output
1 6 3 7 4 9 5 7 8 3
prim:用了一个技巧,把已经联通的路距离构图时距离记为 0 ,
代码:
#include<cstdio>
#include<cstring>
#define MAXN 800
#define INF 0x3f3f3f
#include<algorithm>
using namespace std;
int dis[MAXN];
bool vis[MAXN];
int map[MAXN][MAXN];
int mark[MAXN];
void prim(int n)
{
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[1]=0;
for(int i=1;i<=n;i++)
{
int k=-1;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&(k==-1||dis[j]<dis[k]))
k=j;
}
vis[k]=1;
if(dis[k]!=INF&&dis[k]!=0)
printf("%d %d\n",mark[k],k);
for(int j=1;j<=n;j++)
{
if(!vis[j]&&dis[j]>map[k][j])
{
dis[j]=map[k][j];
mark[j]=k;
}
}
}
}
int main()
{
int n,m,a,b,x[MAXN],y[MAXN];
while(~scanf("%d",&n))
{
memset(map,INF,sizeof(map));
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x[i],&y[i]);
}
for(int i=1;i<n;i++)
{
for(int j=i;j<=n;j++)
{
map[i][j]=map[j][i]=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
}
}
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
map[a][b]=map[b][a]=0; //已经有通路
}
prim(n);
}
return 0;
}
相关文章推荐
- 最小生成树——Prim算法
- 第七章-图(5)图的最小生成树(Kruskal和Prim算法)
- 用于最小生成树的Prim算法实现
- POJ-2031 最小生成树(prim算法)
- 最小生成树(prim算法)
- hihoCoder 1097 : 最小生成树一·Prim算法
- Prim算法 ----最小生成树
- 最小生成树之Prim算法
- prim算法构造最小生成树(邻接表和数组两种存储方式实现)
- 最小生成树--prim算法
- 最小生成树Prim算法
- B - Networking——最小生成树_Prim算法(未堆优化)
- Prim算法求最小生成树
- 最小生成树prim算法
- HDU 1102 Constructing Roads (最小生成树 Prim算法)
- C语言——Prim算法实现最小生成树
- 最小生成树——prim算法实现
- POJ 1258 Agri-Net(最小生成树prim算法)
- hdu 题目 (最小生成树 prim算法)
- POJ 1789 Truck History (Prim算法求最小生成树)