hdu 1217 Arbitrage (spfa演算法)

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   資料   for   

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1217

題目大意:通過貨幣的轉換,來判斷是否獲利,如果獲利則輸出Yes,否則輸出No。

這裡介紹一個STL中的map容器去處理資料,map<string,int>V,M;

現在我目前的理解是將字串轉換成數字,然後就是根據spfa的模板找最短路了。。哇哈哈( ⊙o⊙ )哇

 1 #include <iostream> 2 #include <cstdio> 3 #include <map> 4 #include <queue> 5 #include <cstring> 6 using namespace std; 7 int n,a; 8 double Map[35][35]; 9 const int INF=9999;10 map<string,int>V;11 12 int spfa()13 {14     queue<int>q;15     double node[35]= {0};16     int inq[35]= {0};17     node[1]=1;18     inq[1]=1;19     q.push(1);20     while (!q.empty())21     {22         int s=q.front();23         q.pop();24         for (int i=1; i<=n; i++)25         {26             //cout<<Map[s][i]<<" "<<node[i]<<endl;27             if (node[i]<Map[s][i]*node[s])28             {29                 //cout<<node[i]<<" "<<Map[s][i]<<endl;30                 node[i]=Map[s][i]*node[s];31                 if (!inq[i])32                 {33                     q.push(i);34                     inq[i]=1;35                 }36                 if (node[1]>1)37                     return 1;38             }39 40         }41         inq[s]=0;42     }43     return 0;44 }45 int main ()46 {47     char ch[35];48     int cmp=1,q;49     while (scanf("%d",&n),n)50     {51         q=0;52         V.clear();53         for (int i=1; i<=n; i++)54             for (int j=1; j<=n; j++)55                 Map[i][j]=0;56         //memset(Map,INF,sizeof(Map));57         for (int i=1; i<=n; i++)58         {59             scanf("%s",ch);60             if (!V[ch])61                 V[ch]=++q;62         }63         scanf("%d",&a);64         for (int i=1; i<=a; i++)65         {66             double money;67             char ch1[35],ch2[35];68             scanf("%s%lf%s",ch1,&money,ch2);69             if (Map[V[ch1]][V[ch2]]<money)70                 Map[V[ch1]][V[ch2]]=money;71             //cout<<Map[V[ch1]][V[ch2]]<<endl;72         }73         printf ("Case %d: ",cmp++);74         if (spfa())75             printf("Yes\n");76         else77             printf ("No\n");78     }79     return 0;80 }
View Code

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.