單源最短路 判負環

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   ar   for   art   

Wormholes http://poj.org/problem?id=3259

spfa 2e

 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 #define mt(a,b) memset(a,b,sizeof(a)) 6 using namespace std; 7 const int inf=0x3f3f3f3f; 8 const int M=1024; 9 class Spfa { ///單源最短路o(2*ME)10     typedef int typec;///邊權的類型11     static const int ME=1000010;///邊的個數12     static const int MV=1000010;///點的個數13     struct E {14         int v,next;15         typec w;16     } e[ME];17     int n,le,head[MV],inque[MV];18     typec dist[MV];19     bool used[MV];20     queue<int> q;21 public:22     void init(int tn) { ///傳入點的個數23         n=tn;24         le=0;25         mt(head,-1);26     }27     void add(int u,int v,typec w) {28         e[le].v=v;29         e[le].w=w;30         e[le].next=head[u];31         head[u]=le++;32     }33     bool solve(int s) { ///傳入起點,下標0開始,存在負環返回false34         for(int i=0; i<n; i++) {35             dist[i]=inf;36             used[i]=true;37             inque[i]=0;38         }39         used[s]=false;40         dist[s]=0;41         inque[s]++;42         while(!q.empty()) q.pop();43         q.push(s);44         while(!q.empty()) {45             int u=q.front();46             q.pop();47             used[u]=true;48             for(int i=head[u]; ~i; i=e[i].next) {49                 int v=e[i].v;50                 if(dist[v]>dist[u]+e[i].w) {51                     dist[v]=dist[u]+e[i].w;52                     if(used[v]) {53                         used[v]=false;54                         q.push(v);55                         inque[v]++;56                         if(inque[v]>n) return false;57                     }58                 }59             }60         }61         return true;62     }63     typec getdist(int id) {64         return dist[id];65     }66 } gx;67 68 int main() {69     int t,n,m,w,x,y,z;70     while(~scanf("%d",&t)) {71         while(t--){72             scanf("%d%d%d",&n,&m,&w);73             gx.init(n);74             while(m--) {75                 scanf("%d%d%d",&x,&y,&z);76                 x--;77                 y--;78                 gx.add(x,y,z);79                 gx.add(y,x,z);80             }81             while(w--){82                 scanf("%d%d%d",&x,&y,&z);83                 x--;84                 y--;85                 gx.add(x,y,-z);86             }87             if(!gx.solve(0)) puts("YES");88             else puts("NO");89         }90     }91     return 0;92 }
View Code

 

 

end

單源最短路 判負環

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.