【链接】
http://codeforces.com/gym/101572/attachments
【题意】
找最小环
【思路】
ffloyd跑一边,记录转移路径
【代码】
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1004;
const int inf = 0x3f3f3ff3;
char s[maxn][10];
map<string, int>mp;
int g[maxn][maxn];
int path[maxn][maxn];
int n;
void init() {
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
g[i][j] = inf;
path[i][j] = j;
}
}
}
void floyd() {
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (g[i][k] == inf || g[k][j] == inf)continue;
int tmp = g[i][k] + g[k][j];
if (g[i][j] > tmp) {
g[i][j] = tmp;
path[i][j] = path[i][k];
}
}
}
}
}
void print() {
int minv = inf;
int st, ed;
for (int i = 1; i <= n; i++) {
if (g[i][i] < minv) {
st = ed = i;
minv = g[i][i];
}
}
if (minv == inf)printf("SHIP IT\n");
else {
printf("%s", s[st]);
int u = path[st][ed];
while (u != ed) {
printf(" %s", s[u]);
u = path[u][ed];
}
}
}
char str[100006];
int main() {
scanf("%d", &n);
init();
for (int i = 1; i <= n; i++) {
scanf("%s", s[i]);
mp[s[i]] = i;
}
for (int i = 1; i <= n; i++) {
char ss[10];
scanf("%s", ss);
int m;
scanf("%d", &m);
while (m--) {
char op[10];
scanf("%s", op);
fgets(str, 100000, stdin);
int cnt = 0;
int len = strlen(str);
for (int j = 0; j < len; j++) {
if (str[j] == ' ')cnt = 0;
else if (str[j] == ',' || str[j] == '\n') {
op[cnt] = '\0';
g[mp[ss]][mp[op]] = 1;
}
else {
op[cnt++] = str[j];
}
}
}
}
floyd();
print();
//scanf("%d", &n);
}