POJ 2983 (差分約束系統Spfa)

來源:互聯網
上載者:User

題意:有一些關於點的資訊,大概如下。P A B C 表示點A在點B的北方且與B相距C。 V A B 表示點A在B的北方,但是不清楚具體的距離(A-B>=1)。
題解:A-B=C轉換成A-B>=C和A-B<=C就可以了。

#include <iostream>using namespace std;#define N  200005#define INF 99999999int n, m, size;bool mark[N];int head[N], dis[N];int que[N], inque[N];struct Edge { int v, w, next; };Edge  edge[N];bool Spfa(){memset(mark,0,sizeof(mark));memset(inque,0,sizeof(inque));for ( int i = 0; i <= n; i++ )dis[i] = INF;int u, v, front, rear;front = rear = 0;mark[0] = true;inque[0]++;dis[0] = 0;que[rear] = 0;rear = ( rear + 1 ) % N;while ( front != rear ){u = que[front];front = ( front + 1 ) % N;mark[u] = false;for ( int i = head[u]; i; i = edge[i].next ){v = edge[i].v;if ( dis[v] > dis[u] + edge[i].w ){dis[v] = dis[u] + edge[i].w;if ( ! mark[v] ){que[rear] = v;rear = ( rear + 1 ) % N;mark[v] = true;inque[v]++;if ( inque[v] >= n+1 ) return false;}}}}return true;}void add ( int u, int v, int w ){size++;edge[size].v = v;edge[size].w = w;edge[size].next = head[u];head[u] = size;}int main(){char ch;int a, b, c;while ( scanf("%d%d",&n, &m) != EOF ){size = 0;memset(head,0,sizeof(head));while ( m-- ){getchar();scanf("%c",&ch);if ( ch == 'P' ){scanf("%d%d%d",&a,&b,&c);add ( a, b, -c );add ( b, a, c );}else{scanf("%d%d",&a,&b);add ( a, b, -1 );}}for ( int i = 1; i <= n; i++ )add ( 0, i, 0 );if ( Spfa() )printf("Reliable\n");else    printf("Unreliable\n");}return 0;}

聯繫我們

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