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();
    	}
    }
    
  • 相关阅读:
    [人物存档]AI【捏脸数据】【捏人数据】【人物卡精选】少女3DCG设计参考萌萌的耳朵
    [人物存档]AI【捏脸数据】【捏人数据】【人物卡精选】少女3DCG设计参考
    碧蓝航线-cosplay-吾妻猉 绘画CG设计参考
    Pixiv日榜2020-4-5精选动漫插画壁纸 设计参考
    Marvelous Designer 旗袍设计参考素材免费分享及服装3D模型导入MD教程
    [人物存档]AI【捏脸数据】【捏人数据】【人物卡精选】少女设计参考
    P站日榜【2020-3-22】pixiv动漫美图热门插画壁纸
    Pixiv腿部渔网袜绘画设计参考特辑,在我眼里你就是一个弟弟(姐弟特辑)绘画参考
    Daz3D studio 快速上手进阶免费教程,持续更新
    Airflow 1.10安装
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3412968.html
Copyright © 2011-2022 走看看