首页 > 代码库 > zoj2770差分约束

zoj2770差分约束

#include<stdio.h>#include<iostream>#include<string.h>#include<queue>#include<stack>#include<list>#include<stdlib.h>#include<algorithm>#include<vector>#include<map>#include<set>#include <fstream>using namespace std;const int maxn=1111;int head[maxn];int len;struct Node{    int to;int val;int next;}e[1111111];void add(int from,int to,int val){    e[len].to=to;e[len].val=val;    e[len].next=head[from];    head[from]=len++;}int vis[maxn];int dis[maxn];int cnt[maxn];int n,m;const int INF=0xfffffff;int spfa(int x){    for(int i=0;i<=n;i++){        dis[i]=INF;vis[i]=0;    }    dis[x]=0;vis[x]=1;    queue<int> q; q.push(x);    cnt[x]++;    while(!q.empty()){        int cur=q.front();        q.pop();vis[cur]=0;        for(int i=head[cur];i!=-1;i=e[i].next){            int cc=e[i].to;            if(dis[cc]>dis[cur]+e[i].val){                dis[cc]=dis[cur]+e[i].val;                if(!vis[cc]){                    vis[cc]=1;cnt[cc]++;if(cnt[cc]>n) return 0;                 //   cout<<cc<<" "<<cnt[cc]<<endl;system("pause");                    q.push(cc);                }            }        }    }    return 1;}int main(){    while(cin>>n>>m){        memset(head,-1,sizeof(head));        memset(cnt,0,sizeof(cnt));        len=0;        for(int i=1;i<=n;i++){            int a;cin>>a;            add(i-1,i,a);            add(i,i-1,0);        }        for(int i=0;i<m;i++){            int a;int b;int c;            cin>>a>>b>>c;            add(b,a-1,-c);        }        int t=spfa(n);        if(!t) cout<<"Bad Estimations"<<endl;        else cout<<-dis[0]<<endl;    }    return 0;}