[USACO05DEC] Layout

Title Description

N-cattle, between the two restrictions, one is not more than the distance x, the other is not less than the distance x, n 1 and seeking the maximum number of distance, if there is output program -1 infinite distance may output -2;

2 N . 1 0 0 0, two restriction . 1 M L M D . 1 0 . 4

answer

Differential constraints bare title;

It requires maximum distance, so the shortest? Because it seems to be the shortest path until the condition is reduced from the maximum value, so in the end it must be maximum.

This question is the place to note is negative ring may not be communicated and 1, so only 1 run from spfa possible sentence of less than negative ring.

To forfeit ring only build virtual point to point even all sides, ran spfa point forfeit from the virtual ring.

Finally, from a shortest run spfa request, if dis [n] is not updated, it means that no two constraints.

#include<bits/stdc++.h>
using namespace std;

#define ll long long
const int oo=2000000000;
const int maxn=1005;
const int maxm=21005;
int n,m1,m2;
int head[maxn],cnt;
struct edge{
    int y,next;
    ll val;
}e[maxm];

template<class T>inline void read(T &x){
    x=0;char ch=getchar();
    while(!isdigit(ch)) ch=getchar();
    while(isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
}

void add(int x,int y,ll z){
    e[++cnt]=(edge){y,head[x],z};
    head[x]=cnt;
}

queue<int> q;
ll dis[maxn],cx[maxn];
bool vis[maxn];

void spfa(int s){
    while(!q.empty()) q.pop();
    for(int i=0;i<=n;i++) dis[i]=oo,cx[i]=0,vis[i]=false;
    dis[s]=0;vis[s]=true;cx[s]=1;
    q.push(s);
    while(!q.empty()){
        int x=q.front();
        q.pop();
        vis[x]=false;
        for(int i=head[x];i;i=e[i].next){
            int y=e[i].y;
            if(dis[y]>dis[x]+e[i].val){
                dis[y]=dis[x]+e[i].val;
                if(!vis[y]){
                    q.push(y);vis[y]=true;
                    if(++cx[y]>n) {printf("-1");exit(0);}
                }
            }
        }
    }
}

int main(){
    read(n);read(m1);read(m2);
    for(int i=1;i<=m1;i++){
        int x,y;
        ll z;
        //dis[y]-dis[x]<=z
        //dis[y]<=dis[x]+z
        read(x);read(y);read(z);
        add(x,y,z);
    }
    for(int i=1;i<=m2;i++){
        int x,y;
        ll z;
        //dis[y]-dis[x]>=z
        //dis[x]<=dis[y]-z
        read(x);read(y);read(z);
        add(y,x,-z);
    }
    for(int i=1;i<=n;i++) add(0,i,0);
    spfa(0);
    spfa(1);
    printf("%lld"It's [a] == oo? - 2 : it's [a]); 
}
View Code

Guess you like

Origin www.cnblogs.com/sto324/p/11234725.html