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

来源:互联网 发布:淘宝企业店铺扶持政策 编辑:程序博客网 时间:2024/04/28 01:34

题目地址: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++);    }}


0 0