poj2983 Is the Information Reliable?

2656 ワード

タイトル:
n個の点のm個の制約情報を与える.各情報は、(P a b c)aがb北方距離cにある位置、または(V a b)がb北方1単位距離以上にある位置を表す.以上のm個の要求に合致する点がある可能性があるかを問う.
問題解決の考え方:
差分制約は一般的に私に範囲的な式が不等式を書くことができるという印象を与えますが、この問題では、固定された距離は不等式番号の反対の式2つで表すことができ、このように値を「=」の値に限定することができます.
dis[i]を始点までの距離とする.第2の条件は、dis[a]−dis[b]>=1、すなわちdis[b]<=dis[a]−1である.1つ目について,等しい番号を持つ条件dis[a]−dis[b]=cについて,dis[a]−dis[b]>=cとdis[a]−dis[b]<=cの2つの不等式に変換し,次いで最短三角不等式に変換することができる.複数の不連通図が発生する可能性があるため、仮想始点を1つ設定し、すべての点に接続し、長さを0に設定します.次にspfaを用いて実行可能な解があるか否かを判定する.
コード:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<ctime>
#include<bitset>
#define LL long  long
#define db double
#define EPS 1e-8
#define inf 1e9

using namespace std;

const int nMax=200050;
const int mMax=1000050;
struct{
    int u,v,nxt;
    int  w;
}e[mMax];
int n,cnt,head[nMax];
int dis[nMax];
int que[nMax],m,sum[nMax];
bool vis[nMax];

void add(int u,int v,int w){
    e[cnt].w=w;
    e[cnt].u=u; e[cnt].v=v;
    e[cnt].nxt=head[u];
    head[u]=cnt; cnt++;
}
bool spfa(int s){
    int i,hhead=0,tail=1;
    for (i=0;i<=n;i++){
        dis[i]=inf;
        vis[i]=0;
    }
    dis[s]=0; vis[s]=1; que[0]=s;
    while (hhead!=tail){
        int u=que[hhead];
        vis[u]=0;
        for (int p=head[u];p!=0;p=e[p].nxt){
            int v=e[p].v;
            if (dis[v]>dis[u]+e[p].w){
                dis[v]=dis[u]+e[p].w;
                if (!vis[v]){
                    vis[v]=1;
                    que[tail++]=v;
                    if (tail==nMax) tail=0;
                    if (++sum[v]>n) return 0;
                }
            }
        }
        hhead++;
        if (hhead==nMax) hhead=0;
    }
    return 1;
}
int main(){
    int m,a,b,c,s;
    char str[20];
    while (cin>>n>>m){
        s=0; cnt=1;
        memset(sum,0,sizeof(sum));
        memset(head,0,sizeof(head));
        memset(vis,0,sizeof(vis));
        while (m--){
            scanf("%s",str);
            if (str[0]=='P'){
                scanf("%d%d%d",&a,&b,&c);
                add(b,a,c);
                add(a,b,-c);
            }
            else {
                scanf("%d%d",&a,&b);
                add(a,b,-1);
            }
        }
        for (int i=1;i<=n;i++){
            add(s,i,0);
        }
        if (spfa(s))
            printf("Reliable
"); else printf("Unreliable
"); } return 0; }