zoukankan      html  css  js  c++  java
  • poj 2560,mst

    C. Freckles

    2000ms
    2000ms
    65536KB
    64-bit integer IO format: %lld      Java class name: Main
    Font Size:

    In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley's engagement falls through.

    Consider Dick's back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.


    Input

    The first line contains 0 < n <= 100, the number of freckles on Dick's back. For each freckle, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the freckle.

    Input contains multiple test cases. Process to the end of file.


    Output

    Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.


    Sample Input

    3
    1.0 1.0
    2.0 2.0
    2.0 4.0


    Sample Output

    3.41 kruskal

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<cstdlib>
    #include<cstring>
    #include<fstream>
    
    #define MAX_INT 0x7fffffff
    #define LL long long
    #define ULL unsigned long long
    #define MAX(x,y) ((x) > (y) ? (x) : (y))
    #define MIN(x,y) ((x) > (y) ? (y) : (x))
    
    using namespace std;
    
    #define N 111
    int u[N*N],v[N*N];
    double w[N*N];
    int n,m,p[N],r[N*N];
    
    double x[N],y[N];
    
    bool cmp(int xx, int yy){
        return w[xx] < w[yy];
    }
    
    int findfa(int xx){ return p[xx] == -1 ? xx : (p[xx]=(findfa(p[xx])));}
    
    double kruskal(){
        memset(p,-1,sizeof(p));
        int i,j;
        double ans=0;
        for(j=i=0; i<m; i++){
            int e=r[i];
            int p1=findfa(u[e]), p2=findfa(v[e]);
            if(p1!=p2){
                ans+=w[e];
                p[p1]=p2;
                j++;
                if(j==n-1) return ans;
            }
        }
        return ans;
    }
    
    int main(){
        // fstream fin("C:\Users\Administrator\Desktop\in.txt",ios::in);
    
        while(scanf(" %d",&n)==1){
            int i,j;
            for(i=0; i<n; i++){
                scanf(" %lf %lf",&x[i],&y[i]);
            }
    
            if(n==1){
                printf("0.00
    ");
                continue;
            }
    
            m=0;
            for(i=0; i<n; i++)
                for(j=i+1; j<n; j++){
                    w[m]=sqrt((x[i]-x[j])*(x[i]-x[j]) +
                              (y[i]-y[j])*(y[i]-y[j]));
                    u[m]=i; v[m]=j;
                    m++;
                }
    
            for(i=0; i<m; i++) r[i]=i;
            sort(r,r+m,cmp);
    /*
            for(i=0; i<m ;i++){
                cout<<' '<<w[r[i]];
            }
            cout<<endl;
    */
            printf("%.2f
    ",kruskal());
        }
    
        // fin.close();
        return 0;
    }
    

     prim:

    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstdio>
    #include<iomanip>
    
    using namespace std;
    
    const int maxn = 105;
    const double inf = 1 << 30;
    double x[maxn], y[maxn], w[maxn][maxn], dis[maxn];
    int c[maxn], n;
    bool s[maxn];
    
    int main(){
        while(cin>>n){
            int i, j;
            for(i=0; i<n; i++) cin>>x[i]>>y[i];
            for(i=0; i<n; i++)
                for(j=i+1; j<n; j++)
                    w[i][j]=w[j][i]=sqrt((x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]));
            fill_n(s, n, false);  s[0]=true;
            for(i=1; i<n; i++) dis[i]=w[0][i], c[i]=0;
            double ans = 0;
            while(true){
                int nxt = -1;   double tdis = inf;
                for(i=0; i<n; i++) if(!s[i] && dis[i]<tdis)
                    nxt = i,  tdis = dis[i];
                if(nxt == -1) break;
                ans += tdis;    s[nxt] = true;
                for(i=0; i<n; i++) if(!s[i] && dis[i]>w[nxt][i])
                    dis[i]=w[nxt][i], c[i] = nxt;
            }
            printf("%.2f
    ", ans);
        }
        return 0;
    }
    

     --java prime lite:

    import java.util.Arrays;
    import java.util.Scanner;
    
    
    public class Main {
    	private void poj2560(){
    		Scanner sc = new Scanner(System.in);
    		while(sc.hasNext()){
    			int n = sc.nextInt();
    			double[] x = new double[n+1], y = new double[n+1];
    			for(int i = 0; i < n; i++){ x[i] = sc.nextDouble(); y[i] = sc.nextDouble();}
    			boolean[] s = new boolean[n+1];		Arrays.fill(s, false);
    			double[][] c = new double[n+1][n+1];
    			for(int i = 0; i < n; i++)
    				for(int j = i + 1; j < n; j++)
    					c[i][j] = c[j][i] = Math.sqrt((x[i]-x[j])*(x[i]-x[j]) +
    							(y[i]-y[j])*(y[i]-y[j]));
    			s[0] = true;
    			double[] d = new double[n+1];		double answer = 0;
    			for(int k = 0; k < n; k++) d[k] = c[0][k];
    			for(int k = 1; k < n; k++){
    				int cur = -1;	double min = 100000000;
    				for(int i = 1; i < n; i++) if(!s[i] && d[i]<min){
    					min = d[i];	 cur = i;
    				}
    				s[cur] = true;	answer += d[cur];
    				for(int i = 1; i < n; i++) if(!s[i] && c[cur][i]<d[i])
    					d[i] = c[cur][i];
    			}
    			System.out.printf("%.2f
    ", answer);
    		}
    	}
    	
    	public static void main(String[] args) {
    		new Main().poj2560();
    	}
    }
    
  • 相关阅读:
    MFC中ON_COMMAND,ON_MESSAGE,ON_NOTIFY的区别
    BIOS和CMOS的区别
    强大工具psexec工具用法简介
    Win10如何搭建FTP服务器以实现快速传输文件
    WPA-PSK无线网络破解原理及过程
    Wifi密码破解实战
    洋流.地球:四季只剩下冬天吗?
    天文.地球:最多的陨石有多大?
    德芙背后刻骨铭心的痛
    TTS
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3412968.html
Copyright © 2011-2022 走看看