JZOJ 4671. [increase] A group simulation game T1 World Tour

Subject to the effect:

Given a directed graph, the longest point from the i-th to the j-th point XX will take the shortest path, asking the four cities where to make his way to go.

Problem-solving ideas:

bfs multi-source shortest seek, then the distance is determined for each time point, and the point furthest away its points, the farthest point and the anti-anti-times far point.

After the enumeration between two points, then if it is the farthest the same time with a far, far otherwise are used.

A c c e p t e d   c O d e Accepted\ code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>

const int N = 5100;
const int M = 10100; 

struct edge {
	int to, next; 
}a[M]; 

int n, tot, m, maxs, ls[N]; 
int f[N][N], disnd[N], disnd2[N], disst[N], disst2[N], ans[4];

std::queue <int>  q;

void adde(int x, int y) {
	a[++tot].to = y;  a[tot].next = ls[x];  ls[x] = tot;
}

void bfs(int s) {
	q.push(s); f[s][s] = 0;
	while(!q.empty()) {
		int x = q.front(); q.pop();
		for(int i = ls[x]; i; i = a[i].next) {
			int y = a[i].to;
			if(f[s][y] < 0) {
				f[s][y] = f[s][x] + 1;
				q.push(y);
			}
		}
	}
}

int main() {
	memset(f, 0xcf, sizeof(f));
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= m; ++i) {
		int x, y; 
		scanf("%d %d", &x, &y);
		adde(x, y); 
	}
	for(int i = 1; i <= n; ++i)
		bfs(i);
	for(int i = 1; i <= n; ++i) {
	    for(int j = 1; j <= n; ++j)
			if (i != j) {
		    	if(f[i][j] > f[i][disst[i]]){
		    		disst2[i] = disst[i];
		    		disst[i] = j;
		    	}
		    	else if(f[i][j] > f[i][disst2[i]])
		    		     disst2[i] = j;
		    }
	}
	for(int i = 1; i <= n; ++i) {
	    for(int j = 1; j <= n; ++j)
			if (i != j) {
		    	if(f[j][i] > f[disnd[i]][i]){
		    		disnd2[i] = disnd[i];
		    		disnd[i] = j;
		    	}
		    	else if(f[j][i] > f[disnd2[i]][i])
		    			 disnd2[i] = j;
		    }
	}
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= n; ++j)
	    	if(i != j) {
		    	if(!disnd[i] || !disst[j] || disnd[i] == j || disst[j] == i) continue;
		  	  	if(disnd[i] == disst[j]) {
		  	  	  	if(f[disnd2[i]][i] + f[j][disst[j]] + f[i][j] > maxs && disnd2[i]) {
		  	  	  	 	maxs = f[disnd2[i]][i] + f[j][disst[j]] + f[i][j];
		  	  	  	  	ans[0] = disnd2[i]; ans[1] = i;
		  	  	  	  	ans[2] = j; ans[3] = disst[j];
		  	  	 	}
		  		  	if(f[disnd[i]][i] + f[j][disst2[j]] + f[i][j] > maxs && disst2[j]) {
		  	  	  	  	maxs = f[disnd[i]][i] + f[j][disst2[j]] + f[i][j];
		  	  	  	  	ans[0] = disnd[i]; ans[1] = i;
		  	  	  	  	ans[2] = j; ans[3] = disst2[j];
		  	  		}
		  	  	}
		  	  	else
		  	    	if(f[disnd[i]][i] + f[j][disst[j]] + f[i][j] > maxs) {
		  	  	  	  	maxs = f[disnd[i]][i] + f[j][disst[j]] + f[i][j];
		  	  	  	  	ans[0] = disnd[i];
						ans[1] = i;
		  	  	  	  	ans[2] = j; ans[3] = disst[j];
		  	  		}
		  	}
	return printf("%d %d %d %d\n", ans[0], ans[1], ans[2], ans[3]) & 0;
}

Guess you like

Origin blog.csdn.net/qq_39798042/article/details/88848125