Arbitrage swustoj699
题意
最短路正权环检测。
思路
跟poj1860差不多,都是正权环检测。
poj1860 Currency Exchange传送门1860 – Currency Exchange (poj.org)
除了起点以外,dis都应该初始化为0或者很小的数。
void solve(/* arguments */) {
int n,cnt=0;
while (cin>>n&&n) {
std::map<string, int> ma;
qwe(i,1,n) {
string s;cin>>s;
ma[s]=i;
}
std::vector<std::vector<double> > e(n+1,std::vector<double>(n+1));
std::vector<double> dis(n+1),inq(n+1),ans(n+1);
queue<int> q;
int m=rr(),flag=0;
qwe(i,1,m) {
double rate;
string s1,s2; cin>>s1>>rate>>s2;
e[ma[s1]][ma[s2]]=rate;
}
// spfa
dis[1]=10;
q.push(1);
while (!q.empty()) {
int u=q.front();
q.pop();
inq[u]=0;
for(int i=1;i<=n;i++){
if(dis[i]<dis[u]*e[u][i]){
dis[i]=dis[u]*e[u][i];
ans[i]=ans[u]+1;
if(ans[i]>=n) { // 正权环检测
flag=1;
goto F;
}
if(!inq[i]) {
q.push(i);inq[i]=1;
}
}
}
}
F:
if(flag) std::cout << "Case " << ++cnt << ": Yes" << '\n';
else std::cout << "Case " << ++cnt << ": No" << '\n';
}
}