POJ1364

来源:互联网 发布:怎么查看软件注册表 编辑:程序博客网 时间:2024/05/22 03:19

Problem: King
Description: 国王给出了一些序列和的不等式。现在问你这些不等式是不是都成立。
Solution: 这个题的描述就很郁闷,下标根本没有表达清楚。实际上就是求数列的某一段和。我们设T(n)=S1+S2++Sn, 那么这中间一段[a,b]的和就可以表示成T[b]T[a1]。这样就转化成了差分约束问题。如果出现负环,那么就说明其中有些不等式不对而导致无解。但是这个题目还需要变形,我们知道标准的差分约束是ab<=c,而这个题目中有可能出现>,>=,<。对于大于我们两边同时乘以1就好了;对于没有等号来说,由于题目中给出的c是整数,那么我们可以ab<=c1。这题还有一个很重要的地方,就是要建立一个超级源点来保证这个图的连通。特别注意,以前没有接触过。现在掌握了这种方法。
Code(C++):

#include <iostream>#include <string>#include <queue>using namespace std;const int M=2*100+5;const int INF=0x3f3f3f3f;typedef struct tagNode{    int to,c;    int next;}Node;Node map[M];int head[M];int top,m;int I;int dis[M];int de[M];bool used[M];void add_edge(int from,int to,int c){    map[I].to=to;    map[I].c=c;    map[I].next=head[from];    head[from]=I++;}bool spfa(int src){    for(int i=0;i<M;i++)        dis[i]=INF,de[i]=0,used[i]=false;    queue<int> que;    que.push(src);    de[src]=1;    used[src]=true;    dis[src]=0;    while(!que.empty()){        int pre=que.front();        que.pop();        used[pre]=false;        for(int i=head[pre];i+1;i=map[i].next){            int tmp=map[i].to;            if(dis[tmp]>dis[pre]+map[i].c){                dis[tmp]=dis[pre]+map[i].c;                if(!used[tmp]){                    used[tmp]=true;                    que.push(tmp);                    ++de[tmp];                    if(de[tmp]>top+1)                        return false;                }            }        }    }    return true;}int main(){    while(cin>>top,top)    {        I=0;        cin>>m;        int s,n,k;        string tmp;        for(int i=0;i<M;i++)            head[i]=-1;        for(int i=0;i<top+2;i++)            add_edge(top+1,i,0);        for(int i=0;i<m;i++){            cin>>s>>n>>tmp>>k;            if(tmp=="lt")                //map[s+n][s-1]=k-1;                add_edge(s-1,s+n,k-1);            else                //map[s-1][s+n]=-k-1;                add_edge(s+n,s-1,-k-1);        }        bool f=spfa(top+1);        cout<<(f? "lamentable kingdom":"successful conspiracy")<<endl;    }    return 0;}
0 0
原创粉丝点击