• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            O(1) 的小樂

            Job Hunting

            公告

            記錄我的生活和工作。。。
            <2012年11月>
            28293031123
            45678910
            11121314151617
            18192021222324
            2526272829301
            2345678

            統(tǒng)計(jì)

            • 隨筆 - 182
            • 文章 - 1
            • 評(píng)論 - 41
            • 引用 - 0

            留言簿(10)

            隨筆分類(70)

            隨筆檔案(182)

            文章檔案(1)

            如影隨形

            搜索

            •  

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            POJ 1860 SPFA 最長(zhǎng)路

            貨幣兌換問題,經(jīng)典問題,這個(gè)問題解決的關(guān)鍵是發(fā)現(xiàn),如果存在正環(huán),那么一定是YES。稍微改一下SPFA,尋找一個(gè)圖中的正環(huán)。

             

               1:  /**
               2:  search the longest path , just jude whether there are a positve cycle.
               3:  
               4:  */
               5:   
               6:  #include <queue>
               7:  #include <iostream>
               8:  #include <string.h>
               9:  #include <stdio.h>
              10:  using namespace std;
              11:  #define V 3000      // vertex
              12:  #define E 1500      // edge
              13:  #define INF 1000000.0f
              14:   
              15:  struct node
              16:  {
              17:      int v, next;
              18:      double R,C,w;
              19:  }pnt[E];
              20:   
              21:  int head[V];
              22:  double  dis[V];
              23:  bool vis[V];
              24:  int  cnt[V];          // the num of the operation of push to Quque. negatvie cycle.
              25:  int e = 0;            // the index of the edge
              26:  int N ;               // the num of the vertex.
              27:  int M ;               // the num of edges
              28:  int src, sink;
              29:  double val;
              30:  void addedge(int  u, int v, double R, double C)
              31:  {
              32:      pnt[e].v = v; pnt[e].w= 0;
              33:      pnt[e].R = R; pnt[e].C = C;
              34:      pnt[e].next = head[u]; head[u] = e++;
              35:  }
              36:  void SPFA_init()
              37:  {
              38:      e=0;
              39:      memset(head, -1,sizeof(head));
              40:      memset(vis, 0 ,sizeof(vis));
              41:      memset(cnt, 0 ,sizeof(cnt));
              42:      for(int i=0; i<=N; i++) dis[i] = 0;
              43:  }
              44:  int SPFA()
              45:  {
              46:      queue<int> Q;
              47:      Q.push(src); vis[src] = 1; dis[src] = val; ++cnt[src]; 
              48:      while(!Q.empty()) 
              49:      {
              50:          int u = Q.front(); Q.pop();  vis[u] = 0;
              51:          for(int i=head[u]; i!=-1; i=pnt[i].next)
              52:          {
              53:              int v = pnt[i].v;
              54:              pnt[i].w = (dis[u] - pnt[i].C)*pnt[i].R - dis[u];
              55:              //cout<<u<<" to "<<v<<" "<<pnt[i].w<<endl;
              56:              if( dis[v] < dis[u] + pnt[i].w  )
              57:              {
              58:                  dis[v] = dis[u] + pnt[i].w;
              59:                  //cout<<u<<" to "<<v<<"  "<<pnt[i].w<<" "<<dis[v]<<endl;
              60:                  if(!vis[v]) { Q.push(v); vis[v] = 1;}
              61:                  if( ++cnt[v] > N) return -1; // negative cycle.
              62:              }
              63:          }
              64:      }
              65:      return 1;
              66:      //if(dis[sink] == INF) return -2;          // can't from src to sink. 
              67:      //return dis[sink];
              68:  }
              69:   
              70:  int main()
              71:  {
              72:      freopen("1860.txt","r",stdin);
              73:      scanf("%d%d", &N , &M);
              74:   
              75:      scanf("%d", &src);
              76:      //cout<<src<<endl;
              77:      scanf("%lf", &val);
              78:      //cout<<val<<endl;
              79:      SPFA_init();
              80:      for(int i=0; i<M; i++)
              81:      {
              82:          int a, b; double Rab, Cab, Rba,Cba;
              83:          scanf("%d%d", &a, &b);
              84:          scanf("%lf%lf", &Rab, &Cab);
              85:          scanf("%lf%lf", &Rba, &Cba);
              86:          addedge(a, b,Rab, Cab);
              87:          addedge(b,a,Rba, Cba);
              88:          //cout<<Rab<<" "<<Cab<<" "<<Rba<<" "<<Cba<<endl;
              89:      }
              90:      int ret = SPFA();
              91:      if(ret == -1)  cout<<"YES"<<endl;
              92:      else cout<<"NO"<<endl;
              93:      return 0;
              94:  }

            posted on 2012-11-09 23:24 Sosi 閱讀(501) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Algorithm

            統(tǒng)計(jì)系統(tǒng)
            久久亚洲中文字幕精品有坂深雪| 久久婷婷五月综合成人D啪| 久久夜色精品国产噜噜麻豆| 狠狠色丁香久久婷婷综合五月| 久久精品中文无码资源站| 一本色道久久88加勒比—综合| 亚洲国产精品一区二区三区久久 | 久久国产免费直播| 国产美女久久精品香蕉69| 久久国产一片免费观看| 亚洲国产另类久久久精品| 一级做a爱片久久毛片| 久久精品国产亚洲av麻豆蜜芽 | 久久本道久久综合伊人| 中文字幕无码精品亚洲资源网久久| 99精品久久久久中文字幕| 中文字幕无码av激情不卡久久| 韩国无遮挡三级久久| 中文字幕久久精品无码| 久久久人妻精品无码一区| 久久精品国产亚洲AV嫖农村妇女| 亚洲精品成人网久久久久久| 91久久精品国产免费直播| 久久久久亚洲AV无码麻豆| 久久亚洲中文字幕精品一区| 久久se这里只有精品| 亚洲国产成人久久精品动漫| 精品国产乱码久久久久久人妻| 久久精品无码一区二区日韩AV| 国产精品成人精品久久久| 97精品国产91久久久久久| 久久综合狠狠综合久久| 久久精品国产亚洲AV影院 | 99麻豆久久久国产精品免费| 亚洲欧美伊人久久综合一区二区| 欧美伊人久久大香线蕉综合69| 久久精品国产精品亚洲艾草网美妙| 国产成人久久精品二区三区| 亚洲精品高清国产一久久| 国产精品一区二区久久精品无码 | 性做久久久久久久久久久|