Think:
1知识点:最短路_spfa()算法——判断环的存在
2反思:
1>要努力做到善于总结,勤于反思
2>吸收空格
以下为Accepted代码
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <queue>
using namespace std;
const int L = 1e3 + 4;
const int N = 44;
double e[N][N], dis[N];
int n, vis[N];
map <string, int> m1;
bool spfa(int x);
int main(){
int m, i, j, k = 1;
char st1[L], st2[L];
double v;
while(scanf("%d", &n) && n){
getchar();
m1.clear();
for(i = 1; i <= n; i++){
scanf("%s", st1);
m1[st1] = i;
}
for(i = 1; i <= n; i++){
for(j = 1; j <= i; j++){
if(i == j)
e[i][j] = 1.0;
else
e[i][j] = e[j][i] = 0;
}
}
scanf("%d", &m);
getchar();
for(i = 1; i <= m; i++){
scanf("%s %lf %s", st1, &v, st2);
e[m1[st1]][m1[st2]] = v;
}
bool flag = false;
for(i = 1; i <= n; i++){
if(spfa(i)){
flag = true;
break;
}
}
if(flag)
printf("Case %d: Yes\n", k++);
else
printf("Case %d: No\n", k++);
}
return 0;
}
bool spfa(int x){
queue <int> q;
while(!q.empty()){
q.pop();
}
memset(vis, 0, sizeof(vis));
memset(dis, 0, sizeof(dis));
vis[x] = 1, dis[x] = 1.0;
q.push(x);
while(!q.empty()){
int t1 = q.front();
q.pop();
vis[t1] = 0;
for(int i = 1; i <= n; i++){
if(dis[t1]*e[t1][i] > dis[i]){
dis[i] = dis[t1]*e[t1][i];
if(dis[x] > 1.0){
return true;
}
if(!vis[i]){
vis[i] = 1;
q.push(i);
}
}
}
}
return false;
}