首页 > 代码库 > UVA 10985 Rings'n'Ropes
UVA 10985 Rings'n'Ropes
最短路 参考了Staingger的博客
感觉DP的状态记录还是有毛病。可以DFS寻找结果也。
#include <map>#include <set>#include <list>#include <cmath>#include <ctime>#include <deque>#include <stack>#include <queue>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <climits>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#define LL long long#define PI 3.1415926535897932626using namespace std;int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}#define MAXN 150const int INF = 0x3f3f3f3f ;int dp[MAXN][MAXN];bool vis[MAXN][MAXN];int N,M,ans;void init(){ scanf("%d%d",&N,&M); memset(dp,0x3f,sizeof(dp)); while (M--) { int u,v; scanf("%d%d",&u,&v); dp[u][v] = dp[v][u] = 1; } for (int k = 0; k < N; k++) for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) dp[i][j] = min(dp[i][j],dp[i][k] + dp[k][j]); for (int i = 0; i < N; i++) dp[i][i] = 0;}int calcu(int s, int t){ int res[MAXN],cas = 0,cnt = 0; for (int k = 0; k < N; k++) if (dp[s][k] + dp[k][t] == dp[s][t]) res[cas++] = k; for (int i = 0; i < cas; i++) for (int j = i + 1; j < cas; j++) { int a = res[i] , b = res[j]; if(dp[a][b] == 1 && dp[s][a] != dp[s][b]) cnt++; } return cnt;}void slove(){ for (int i = 0; i < N; i++) for (int j = i + 1; j < N; j++) { if (dp[i][j] != INF) ans = max(ans,calcu(i,j)); }}int main(){ //freopen("sample.txt","r",stdin); int T,kase = 1; scanf("%d",&T); while (T--) { init(); ans = 0; slove(); printf("Case #%d: %d\n",kase++,ans); } return 0;}
UVA 10985 Rings'n'Ropes
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。