• poj2240Arbitrage


    题目链接:http://poj.org/problem?id=2240

    floyd:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<string>
     4 #include<map>
     5 #include<iostream>
     6 using namespace std;
     7 const int maxn=32;
     8 int n,m,u,v;
     9 double r;
    10 double pic[maxn][maxn];
    11 
    12 void floyd()
    13 {
    14     for(int k=1;k<=n;k++)
    15         for(int i=1;i<=n;i++)
    16             for(int j=1;j<=n;j++)
    17                 pic[i][j]=max(pic[i][j],pic[i][k]*pic[k][j]);
    18 }
    19 
    20 int main()
    21 {
    22     char s1[100],s2[100],s[100];
    23     int cas=0;
    24     while(scanf("%d",&n)!=EOF&&n)
    25     {
    26         int ok=0;
    27         map<string,int> mapp;
    28         mapp.clear();
    29         for(int i=1;i<=n;i++){
    30             scanf("%s",s);
    31             mapp[s]=i;
    32         }
    33         scanf("%d",&m);
    34 
    35          for(int i=1;i<=n;i++)
    36             for(int j=1;j<=n;j++)
    37             pic[i][j]=(i==j?1:0);
    38 
    39         for(int i=0;i<m;i++)
    40         {
    41             scanf("%s%lf%s",s1,&r,s2);
    42             u=mapp[s1];
    43             v=mapp[s2];
    44             pic[u][v]=r;
    45         }
    46 
    47         floyd();
    48         for(int i=1;i<=n;i++)
    49           if(pic[i][i]>1) {ok=1;break;}
    50         printf("Case %d: ",++cas);
    51         if(ok) puts("Yes");
    52         else puts("No");
    53     }
    54     return 0;
    55 }

    也可用bellman-ford判"正环"。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<string>
     4 #include<map>
     5 using namespace std;
     6 const int maxn=32;
     7 double dis[maxn];
     8 double r;
     9 int n,m,u,v;
    10 struct node
    11 {
    12     int u,v;
    13     double r;
    14 }e[1010];
    15 bool bellman_ford()
    16 {
    17     int ok=0;
    18     for(int i=1;i<=n;i++)
    19         dis[i]=0;
    20     dis[1]=1;
    21     for(int i=0;i<n;i++)
    22     {
    23         ok=0;
    24         for(int i=0;i<m;i++)
    25             if(dis[e[i].v]<dis[e[i].u]*e[i].r){
    26                     ok=1;
    27                 dis[e[i].v]=dis[e[i].u]*e[i].r;
    28             }
    29         if(!ok) break;
    30         if(i==n-1) return 1;
    31     }
    32     return 0;
    33 }
    34 
    35 int main()
    36 {
    37     int cas=0;
    38     char s[100],s1[100],s2[100];
    39     while(scanf("%d",&n)!=EOF&&n)
    40     {
    41         map<string,int> mapp;
    42         mapp.clear();
    43         for(int i=1;i<=n;i++)
    44         {
    45             scanf("%s",s);
    46             mapp[s]=i;
    47         }
    48         scanf("%d",&m);
    49         for(int i=0;i<m;i++)
    50         {
    51             scanf("%s%lf%s",s1,&r,s2);
    52             u=mapp[s1];
    53             v=mapp[s2];
    54             e[i].u=u;
    55             e[i].v=v;
    56             e[i].r=r;
    57         }
    58         printf("Case %d: ",++cas);
    59         if(bellman_ford()) puts("Yes");
    60         else puts("No");
    61     }
    62 }

    或者spfa。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<string>
     4 #include<map>
     5 #include<queue>
     6 using namespace std;
     7 
     8 const int maxn=32;
     9 double r,pic[maxn][maxn];
    10 int n,m,u,v;
    11 double dis[maxn];
    12 
    13 bool inq[maxn];
    14 
    15 int spfa(int s)
    16 {
    17     for(int i=1;i<=n;i++){
    18             inq[i]=0;
    19         dis[i]=0;
    20     }
    21 
    22     queue<int> q;
    23     while(!q.empty()) q.pop();
    24     dis[s]=1;
    25     q.push(s);
    26     inq[s]=1;
    27     while(!q.empty())
    28     {
    29         int x=q.front();
    30         q.pop();
    31         inq[x]=0;
    32         for(int i=1;i<=n;i++)
    33             if(dis[i]<dis[x]*pic[x][i])
    34         {
    35             dis[i]=dis[x]*pic[x][i];
    36             if(dis[s]>1.0) return 1;
    37             if(!inq[i])
    38             {
    39                 inq[i]=1;
    40                 q.push(i);
    41             }
    42         }
    43     }
    44     return 0;
    45 
    46 }
    47 int main()
    48 {
    49     int cas=0;
    50     char s[100],s1[100],s2[100];
    51     while(scanf("%d",&n)!=EOF&&n)
    52     {
    53         map<string,int> mapp;
    54         mapp.clear();
    55         for(int i=1;i<=n;i++){
    56             scanf("%s",s);
    57             mapp[s]=i;
    58         }
    59         for(int i=1;i<=n;i++)
    60             for(int j=1;j<=n;j++)
    61             pic[i][j]=(i==j?1:0);
    62         scanf("%d",&m);
    63         for(int i=0;i<m;i++)
    64         {
    65             scanf("%s%lf%s",s1,&r,s2);
    66             u=mapp[s1];
    67             v=mapp[s2];
    68             pic[u][v]=r;
    69         }
    70         int ok=0;
    71         for(int i=1;i<=n;i++)
    72         {
    73             if(spfa(i)==1) {ok=1;break;}
    74         }
    75         printf("Case %d: ",++cas);
    76         if(ok) puts("Yes");
    77         else puts("No");
    78     }
    79 }
  • 相关阅读:
    “中国半导体教父”张汝京:中国半导体只缺人才
    集群搭建
    Scrapy
    商品建模
    python wordcloud
    StaticFileMiddleware中间件如何处理针对文件请求
    Docker / CI / CD
    NET Memory Profiler 跟踪.net 应用内存
    SOS.dll (SOS Debugging Extension)
    Download the WDK, WinDbg, and associated tools
  • 原文地址:https://www.cnblogs.com/yijiull/p/6615991.html
Copyright © 2020-2023  润新知