題意:有一些關於點的資訊,大概如下。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;}