社区讨论

请求加强数据

P1522[USACO2.4] 牛的旅行 Cow Tours参与者 2已保存回复 4

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
4 条
当前快照
1 份
快照标识符
@m515uhz3
此快照首次捕获于
2024/12/23 22:56
去年
此快照最后确认于
2025/11/04 12:25
4 个月前
查看原帖
O(n4)O(n^4) 过了,甚至最久的点只跑了 112ms112ms
CPP
#include <bits/stdc++.h>
#define rint register int
#define rdouble register double
#define N 155
#define abs(a) (a<0?-(a):a)
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
//Debug
//#define PRTBASE
using namespace std;
double Dis[N][N], dis[N][N];
double _dis[N][N];
int col[N][N], belong[N], cnt;
double d[N], ans = 1e20;
bool G[N][N];
int posx[N], posy[N];
char tmp[N];
int n;

inline void cpy(){
	for(rint i = 1; i <= n; ++i){ // Each row
		for(rint j = 1; j < i; ++j){ // Each column
			_dis[i][j] = _dis[j][i] = dis[i][j]; // Copy the data
		}
	}
}
inline void color(rint u, rint c){
	col[c][++col[c][0]] = u; // Add the node to the set
	belong[u] = c; // Set the relationship
	for(rint i = 1; i <= n; ++i){ // For each node
		if(dis[u][i] < 1e9 && !belong[i]){ // If there is a edge between them and this node havn't been searched
		    color(i, c); // Search it
		}
	}
}

signed main(){
    scanf("%d", &n); // Input the number of the nodes
    for(rint i = 1; i <= n; ++i){ // For each node
        scanf("%d %d", &posx[i], &posy[i]); // Input the position
    }
    for(rint i = 1; i <= n; ++i){
        for(rint j = 1; j < i; ++j){ // For each pair of nodes
            rint a = abs(posx[i]-posx[j]), b = abs(posy[i]-posy[j]); // Find the length of a, b
            Dis[i][j] = Dis[j][i] = sqrt(a*a+b*b); // Calc the distance
        }
    }
    for(rint i = 1; i <= n; ++i){
    	scanf("%s", tmp+1); // Input the edges
    	for(rint j = 1; j <= n; ++j){ // For each possible edge
    		dis[i][j] = (tmp[j]-'0') ? Dis[i][j] : 1e9; // If there is a edge here, copy the distance; if there's not, set the distance to infinite to show there is not a edge
    		if(i == j) dis[i][j] = 0; // If the two nodes is the same, the distance is zero.
		}
	}
	for(rint k = 1; k <= n; ++k){ // For each node
		if(!belong[k]) color(k, ++cnt); // If the node haven't been colored, color it
		rint c = belong[k]; // The color
//		/*  
		for(rint i = 1; i <= col[c][0]; ++i){ // For each node which has the same color
			rint u = col[c][i]; // The node
			for(rint j = 1; j <= col[c][0]; ++j){ // For each node which has the same color too
				rint v = col[c][j]; // The node
				dis[u][v] = min(dis[u][k] + dis[k][v], dis[u][v]); // Choose the shortest distance
			}
		}
//		*/
	}
	for(rint c = 1; c <= n; ++c){
		rdouble diam = 0;
		for(rint i = 1; i <= col[c][0]; ++i){
			rint u = col[c][i];
			for(rint j = 1; j <= col[c][0]; ++j){
				rint v = col[c][j];
				diam = max(diam, dis[u][v]);
			}
		}
		d[c] = diam;
	}
	/*
	for(rint k = 1; k <= n; ++k){
		for(rint i = 1; i <= n; ++i){
			for(rint j = 1; j <= n; ++j){
				dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
			} 
		} 
	} 
	*/
	for(rint ci = 1; ci <= cnt; ++ci){
		for(rint cj = ci+1; cj <= cnt; ++cj){ // For each two colors
			for(rint _i = 1; _i <= col[ci][0]; ++_i){
				rint u = col[ci][_i];
				for(rint _j = 1; _j <= col[cj][0]; ++_j){
					rint v = col[cj][_j]; // Choose a node in each color
					rdouble diam = 0; // The new value d of the new color
					cpy(); // Copy the distances
					_dis[u][v] = Dis[u][v]; // Connect the two nodes
					for(rint i_ = 1; i_ <= col[ci][0]; ++i_){
						rint i = col[ci][i_];
						for(rint j_ = 1; j_ <= col[cj][0]; ++j_){ // For the other nodes in the two colors
							rint j = col[cj][j_];
							_dis[i][j] = _dis[j][i] = min(_dis[i][u]+_dis[u][v]+_dis[v][j], _dis[i][j]); // If the distance of the path which crosses the bridge is shorter than before, update the distance
							if(_dis[i][j] < 1e9) diam = max(_dis[i][j], diam); // Update value d
						}
					}
					diam = max(diam, max(d[ci], d[cj])); // The value d in the two old colors may be larger than the new one
					ans = min(ans, diam); // Update the answer
				}
			}
		}
	}
	printf("%.6f", ans); // Output
	return 0;
}

MaxmiliteShineEternalLittle09

回复

4 条回复,欢迎继续交流。

正在加载回复...