您的位置:首页 > 其它

[kuangbin带你飞]专题四 最短路练习 I POJ 2240

2016-10-11 20:43 330 查看
4000

题目地址:https://vjudge.net/contest/66569#problem/I

思路:和之前有道题基本一模一样,就是这里要先处理一下货币名,然后spfa敲一下就可以了。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<map>
#include<string>
using namespace std;
vector<pair<int,double> >E[35];
int time[35];
bool vis[35];
double d[35];
int n,m;

bool spfa(int s)
{
for(int i=0;i<=n;i++)
{
time[i]=0;
d[i]=0;
vis[i]=false;
}
queue<int>q;
q.push(s);
d[s]=1;
vis[s]=true;
while(!q.empty())
{
int now=q.front();
q.pop();
vis[now]=false;
if(time[now]++>n)
return true;
for(int i=0;i<E[now].size();i++)
{
int v=E[now][i].first;
if(d[v]<d[now]*E[now][i].second)
{
d[v]=d[now]*E[now][i].second;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
}
if(d[s]>1)
return true;
return false;
}

int main()
{
int t=1;
while(scanf("%d",&n) && n)
{
map<string,int>mm;
for(int i=1;i<=n;i++)
{
string temp;
cin>>temp;
mm[temp]=i;
E[i].clear();
}
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
string temp1,temp2;
double temp;
cin>>temp1>>temp>>temp2;
int a=mm[temp1];
int b=mm[temp2];
E[a].push_back(make_pair(b,temp));
}
int temp=0;
for(int i=1;i<=n;i++)
{
if(spfa(i))
{
temp=1;
break;
}
}
if(temp)
printf("Case %d: Yes\n",t++);
else
printf("Case %d: No\n",t++);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: