• 布局(codevs 1242)


    题目描述 Description

    当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些。FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食。奶牛排在队伍中的顺序和它们的编号是相同的。因为奶牛相当苗条,所以可能有两头或者更多奶牛站在同一位置上。即使说,如果我们想象奶牛是站在一条数轴上的话,允许有两头或更多奶牛拥有相同的横坐标。

    一些奶牛相互间存有好感,它们希望两者之间的距离不超过一个给定的数L。另一方面,一些奶牛相互间非常反感,它们希望两者间的距离不小于一个给定的数D。给出ML条关于两头奶牛间有好感的描述,再给出MD条关于两头奶牛间存有反感的描述。(1<=ML,MD<=10000,1<=L,D<=1000000)

    你的工作是:如果不存在满足要求的方案,输出-1;如果1号奶牛和N号

    奶牛间的距离可以任意大,输出-2;否则,计算出在满足所有要求的情况下,1号奶牛和N号奶牛间可能的最大距离。

    输入描述 Input Description
    Line 1: Three space-separated integers: N, ML, and MD. 

    Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart. 

    Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.
    输出描述 Output Description
    Line 1: A single integer. If no line-up is possible, output -1. If cows 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between cows 1 and N.
    样例输入 Sample Input

    4 2 1
    1 3 10
    2 4 20
    2 3 3

    样例输出 Sample Output

    27

    *
      差分约束
    */
    #include<cstdio>
    #include<iostream>
    #include<queue>
    #include<cstring>
    #define N 1010
    #define M 20010
    using namespace std;
    int head[N],vis[N],dis[N],ci[N],n,m1,m2,cnt,flag;
    struct node
    {
        int v,t,pre;
    };node e[M];
    void add(int x,int y,int z)
    {
        ++cnt;
        e[cnt].v=y;
        e[cnt].t=z;
        e[cnt].pre=head[x];
        head[x]=cnt;
    }
    void spfa(int s,int t)
    {
        memset(dis,0x3f3f3f3f,sizeof(dis));
        queue<int> q;
        q.push(s);vis[s]=1;dis[s]=0;ci[s]=1;
        while(!q.empty())
        {
            int u=q.front();
            q.pop();vis[u]=0;
            for(int i=head[u];i;i=e[i].pre)
              if(dis[e[i].v]>dis[u]+e[i].t)
              {
                  dis[e[i].v]=dis[u]+e[i].t;
                  if(!vis[e[i].v])
                  {
                      ci[e[i].v]++;
                      vis[e[i].v]=1;
                      q.push(e[i].v);
                      if(ci[e[i].v]==n)
                      {
                          flag=1;
                          return;
                    }
                }
              }
        }
        
    }
    int main()
    {
        scanf("%d%d%d",&n,&m1,&m2);
        for(int i=1;i<=m1;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);
        }
        for(int i=1;i<=m2;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            add(y,x,-z);
        }
        spfa(1,n);
        if(flag)printf("-1");
        else if(dis[n]<1000000000)printf("%d",dis[n]);
        else printf("-2");
        return 0;
    }
    View Code
  • 相关阅读:
    1_Selenium环境搭建
    python functools
    python 参数注解inspect
    python 堆排序
    python functools
    python 装饰器
    python 柯里化
    python 高阶函数
    python 树
    python 函数销毁
  • 原文地址:https://www.cnblogs.com/harden/p/5883089.html
Copyright © 2020-2023  润新知