zoukankan      html  css  js  c++  java
  • POJ 1751 Highways(最小生成树Prim普里姆,输出边)

    题目链接:点击打开链接

    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

    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

    题目大意:直接解读样例:9个村庄的坐标,三个已经有了的公路,输出还要建的公路以使公路总长最短。

    思路:稠密图求最小生成树的问题,既然是稠密图,可以用Prim算法。已经有了的边要加入最小生成树中,就令这些边的权值为零。这题的邻接矩阵cost[][],注意邻接矩阵的主对角线是0, 而且对称。 每条边的权值为两点之间的距离,因为只是比较距离,所以在prim里直接比较距离的平方就行,这样也可以避免sqrt之后变成double出现精度问题。

    还有就是如何输出边的问题,在prim中要求输出边的话,可以新建一个edge[]数组,edge[i] = j表示i是从j延伸过来的,代码中有有三处出现了edge[],仔细思考

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<string>
    #include<set>
    typedef long long ll;
    using namespace std;
    
    const int inf = 0x3f3f3f3f;
    const int maxn = 800;
    bool vis[maxn];
     int edge[maxn];
    int lowc[maxn];
    int cost[maxn][maxn];
    void prim(int cost[][maxn], int n){
    	int ans = 0;
    	bool ard = false;
    	memset(vis, false, sizeof(vis));
    	vis[0] = inf;
    	for(int i = 1; i < n; i++) {lowc[i] = cost[0][i];edge[i] = 0;}/////////////1
    	for(int i = 1; i < n; i++){
    		int minc = inf;
    		int p = -1;
    		for(int j = 0; j < n; j++){
    			if(!vis[j] && minc > lowc[j]){
    				minc = lowc[j];
    				p = j;
    			}
    		}
    		if(p == -1)return ;
    		
    		ans += minc;
    		vis[p] = true;
    		for(int j = 0; j < n; j++){
    			if(!vis[j] && lowc[j] > cost[p][j]) {lowc[j] = cost[p][j];edge[j] = p;}/////////////////2
    			if(edge[p] == j &&cost[p][j] == minc && minc!= 0 ) {printf("%d %d
    ", p+1, j+1);}//////////////3
    		}
    	}
    	return ;
    }
    
    struct Node{
    	int  x, y;
    	
    }node[maxn];
    
    ll d2(int x1, int y1, int x2, int y2){
    	ll ans = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    	return ans;
    }
    
    int main(){
        int n, m;
        
        scanf("%d", &n);
        int p = 0;
        
        for(int i = 0; i < n; i++){
        	scanf("%d %d", &node[p].x, &node[p].y);
        	p++;
    	}
    	scanf("%d", &m);
    
    	int n1, n2;
    	for(int i = 0; i < n; i++){
    		for(int j = 0; j < n; j++){		
    			cost[i][j] = d2(node[i].x, node[i].y,node[j].x, node[j].y);
    		}
    		
    	}
    	for(int i = 0; i < m; i++){
    		scanf("%d %d", &n1, &n2);
    		n1--;n2--;
    		cost[n1][n2] = 0;
    		cost[n2][n1] = 0;
    
    	}
    	//printf("
    ");
    	/*for(int i = 0; i < n; i++){
    	    printf("no[%d] = %d 
    ", i, no[i]);
    	    //for(int j = 0; j < n; j++)printf("[%d][%d]:%d ", i, j, cost[i][j]);
    	    printf("
    ");
    	}
    	printf("
    ");*/
    	prim(cost, n);
    	return 0;
    }


  • 相关阅读:
    hive.exec.parallel参数
    MySQL FEDERATED 提示
    mapreduce作业单元测试
    linux 更改mysql的数据库目录
    SQL Server 2008数据库邮件配置及应用
    mysql主键大小写不敏感的解决办法
    java遍历hashMap、hashSet、Hashtable
    Linux下命令行显示当前全路径方法
    通过SQL Server操作MySQL的步骤和方法
    Linux shell获取时间和时间间隔(ms级别)
  • 原文地址:https://www.cnblogs.com/wrjlinkkkkkk/p/9552009.html
Copyright © 2011-2022 走看看