• Codevs 1242 布局 2005年USACO(差分约束)


    1242 布局 2005年USACO
    时间限制: 1 s
    空间限制: 128000 KB
    题目等级 : 黄金 Gold
    题目描述 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
    数据范围及提示 Data Size & Hint
    分类标签 Tags
    最短路 图论 USACO 2005年

    /*
    差分约束.
    由约束条件可得
    (1)dis[y1]-dis[x1]<=T.
    (2)dis[x2]-dis[y2]<=-D.
    (3)dis[i]-dis[i+1]>=0.
    建图后spfa跑最短路.
    */
    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    #define MAXN 500001
    using namespace std;
    struct data{int v,next,x;}e[MAXN*3];
    int n,m,dis[MAXN],head[MAXN],cut,k,c[MAXN];
    bool b[MAXN];
    int read()
    {
        int x=0;char ch=getchar();
        while(ch<'0'||ch>'9') ch=getchar();
        while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
        return x;
    }
    void add(int u,int v,int z)
    {
        e[++cut].v=v;
        e[cut].x=z;
        e[cut].next=head[u];
        head[u]=cut;
    }
    int spfa()
    {
        queue<int>q;q.push(1);
        memset(dis,127/3,sizeof(dis));
        dis[1]=0;
        while(!q.empty())
        {
            int u=q.front();q.pop();b[u]=false;
            for(int i=head[u];i;i=e[i].next)
            {
                int v=e[i].v;
                if(dis[v]>dis[u]+e[i].x)
                {
                    dis[v]=dis[u]+e[i].x;
                    if(!b[v])
                    {
                        b[v]=true,q.push(v);c[v]++;
                        if(c[v]>=n) return -1;;
                     } 
                }
            }
        }
        if(dis[n]==dis[0]) return -2;
        return dis[n];
    }
    int main()
    {
        int x,y,z;
        n=read(),m=read(),k=read();
        for(int i=1;i<=m;i++)
        {
            x=read(),y=read(),z=read();
            add(x,y,z);
        }
        for(int i=1;i<=k;i++)
        {
            x=read(),y=read(),z=read();
            add(y,x,-z);
        }
        for(int i=1;i<=n;i++) add(i+1,i,0);
        printf("%d",spfa());
        return 0;
    }
  • 相关阅读:
    Android程序反编译的方法(转)
    VMware的Easy Install安装
    Oracle ORA12514:TNS:监听程序当前无法识别连接描述符中请求的服务
    Java Eclipse 如何导入外部Jar包
    Oracle IMP00010: 不是有效的导出文件,标题验证失败
    怎样取消Windows 2003 server 意外关机提示
    没有文件扩展“.vbs”的脚本引擎的解决方案
    Oracle init.ora常用配置详解
    eclipse断点调试 出现Source not found
    VMware Workstation 8
  • 原文地址:https://www.cnblogs.com/nancheng58/p/10068096.html
Copyright © 2020-2023  润新知