C++之路进阶codevs1242(布局)

1242 布局

 

2005年USACO

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold 
 

<:section class="hbox"><:aside class="col-sm-11 bg-white-only">

 
题目描述 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
题解
  差分!!!(我不太会,待学习...)
 1 #include<cstdio>
 2 #include<queue>
 3 #include<iostream>
 4 #include<algorithm>
 5 #define maxn 10000+10
 6 #define INF 0x7fffffff
 7 
 8 using namespace std;
 9 
10 int head[maxn],cnt,dis[maxn],vis[maxn],inq[maxn],n,ml,md;
11 
12 struct ss
13    {
14         int to,next,edge;
15    }e[100000<<1];
16    
17 void insert(int u,int v,int edge)
18    {
19         e[++cnt].to=v,e[cnt].next=head[u],e[cnt].edge=edge,head[u]=cnt;
20    }   
21 
22 int spfa()
23     {
24       queue<int>que;    
25       dis[1]=0;    
26       vis[1]=1;    
27       inq[1]++;
28       que.push(1);
29       int flag=0;    
30       while (!que.empty())
31          {
32              int now=que.front();que.pop();vis[now]=0;
33              for (int i=head[now];i;i=e[i].next)
34                  {
35                     if (dis[e[i].to]>dis[now]+e[i].edge)
36                        {
37                          dis[e[i].to]=dis[now]+e[i].edge;
38                          if (!vis[e[i].to])
39                          {
40                            vis[e[i].to]=1;que.push(e[i].to);
41                            inq[e[i].to]++;
42                            if (inq[e[i].to]>n) return -1;    
43                          } 
44                       }
45                          
46                  }
47          }
48       return dis[n];         
49     }
50 
51 int main()
52    {
53         scanf("%d%d%d",&n,&ml,&md);
54         for (int i=1;i<=ml;i++)
55             {
56                 int x,y,z;
57                 scanf("%d%d%d",&x,&y,&z);
58                 insert(x,y,z);
59             }
60        for (int i=1;i<=md;i++)
61          {
62            int x,y,z;
63            scanf("%d%d%d",&x,&y,&z);
64            insert(y,x,-z);    
65          } 
66     for (int i=1;i<=n;i++) dis[i]=INF;     
67     int ans=spfa();     
68     if (ans==INF) cout<<-2<<endl;
69          else cout<<ans<<endl;           
70     return 0;    
71    }
原文地址:https://www.cnblogs.com/grhyxzc/p/5742642.html