zoukankan      html  css  js  c++  java
  • 洛谷p-1522又是Floyd

    挺简单一个题,可惜当时没想到,有点巧妙丫!

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #define maxn 255
    using namespace std;
    char list[maxn][maxn];
    double map[maxn][maxn];
    int vis[maxn];
    int ins[maxn];
    int n, m;
    struct Node {
    	double x;
    	double y;
    	double len;
    }que[maxn];
    double get(Node a, Node b) {
    	return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
    }
    
    int dfs(int x, int num) {
    	ins[x] = num;
    	vis[x] = 1;
    	for (int i = 0; i < n; i++) {
    		if (!vis[i] && list[x][i] == '1') dfs(i, num);
    	}
    	return 0;
    }
    void floyd() {
    	for (int k = 0; k < n; k++) {
    		for (int i = 0; i < n; i++) {
    			for (int j = 0; j < n; j++) {
    				map[i][j] = min(map[i][j], map[i][k] + map[k][j]);
    			}
    		}
    	}
    }
    int main() {
    	scanf("%d", &n);
    	for (int i =  0; i < n; i++) {
    		scanf("%lf %lf", &que[i].x, &que[i].y);
    	}
    	for (int i = 0; i < n; i++) {
    		scanf("%s", list[i]);
    	}
    
    	for (int i = 0; i < n; i++) {
    		for (int j = 0; j < n; j++) {
    			if (list[i][j] == '0') map[i][j] = 1100000.0;
    			else map[i][j] = get(que[i], que[j]);
    		}
    	}
    	for (int i = 0; i < n; i++) map[i][i] = 0.0;
    	floyd();
    	int cnn = 0;
    	for (int i = 0; i < n; i++) {
    		if (!vis[i]) {
    			cnn++;
    			dfs(i, cnn);
    		}
    	}
    	for (int i = 0; i < n; i++) {
    		que[i].len = 0;
    		for (int j = 0; j < n; j++) {
    			if (ins[i] == ins[j]) {
    				double len = map[i][j];
    				que[i].len = max(que[i].len, len);
    			}
    		}
    	}
    	double ans = 1100000;
    	
    	for (int i = 0; i < n; i++) {
    		for (int j = 0; j < n; j++) {
    			if (ins[i] != ins[j]) {
    				double len = get(que[i], que[j]);
    				len += que[i].len + que[j].len;
    				ans = min(ans, len);
    			}
    		}
    	}
    	for (int i = 0; i < n; i++) {
    		ans = max(que[i].len, ans);//特殊情况,原图自己就已经够大的了,没必要再加了
    	}
    
    	printf("%.6lf
    ", ans);
    	return 0;
    }
    

      

    寻找真正的热爱
  • 相关阅读:
    [Noi2011]阿狸的打字机
    Bzoj3530: [Sdoi2014]数数
    Bzoj2037: [Sdoi2008]Sue的小球
    Bzoj4869: [Shoi2017]相逢是问候
    Bzoj1899: [Zjoi2004]Lunch 午餐
    Bzoj3884: 上帝与集合的正确用法
    UVA10692:Huge Mods
    Bzoj1009: [HNOI2008]GT考试
    Bzoj1212: [HNOI2004]L语言
    【国家集训队2012】tree(伍一鸣)
  • 原文地址:https://www.cnblogs.com/lesning/p/11459741.html
Copyright © 2011-2022 走看看