Burn the Linked Camp 【区间 差分约束】

来源:互联网 发布:初中试题软件下载 编辑:程序博客网 时间:2024/05/29 05:02

It is well known that, in the period of The Three Empires, Liu Bei, the emperor of the Shu Empire, was defeated by Lu Xun, a general of the Wu Empire. The defeat was due to Liu Bei’s wrong decision that he divided his large troops into a number of camps, each of which had a group of armies, and located them in a line. This was the so-called “Linked Camps”.
Let’s Go back to that time. Lu Xun had sent many scouts to obtain the information about his enemy. From his scouts, he knew that Liu Bei had divided his troops into n camps, all of which located in a line, labeled by 1..n from left to right. The ith camp had a maximum capacity of Ci soldiers. Furthermore, by observing the activities Liu Bei’s troops had been doing those days, Lu Xun could estimate the least total number of soldiers that were lived in from the ith to the jth camp. Finally, Lu Xun must estimate at least how many soldiers did Liu Bei had, so that he could decide how many troops he should send to burn Liu Bei’s Linked Camps.
Sample Input:
3 2
1000 2000 1000
1 2 1100
2 3 1300
3 1
100 200 300
2 3 600
Sample Output:
1300
Bad Estimations

题目意思:刘备有n个兵营,每个兵营都有一个容量即住的最大人数。现在陆逊通过观察得到m条信息,从兵营 i 到 j 的最少人数。 问你能否求出刘备兵营里面的最少人数。能的话输出最少人数,不能输出Bad Estimations。

准备工作:dist[i]记录源点到i的最短路 man[i]表示i营地人数容量 total[i]表示前i个营地总容量

思路:在m条信息里面,我们用 first, last, v分别表示开始营地 结束营地 以及两个营地之间最少人数 ,并设第i个营地实际人数为Ai, 前i个营地实际总人数为Si
那么可以得到以下3个关系:
一:第first到第last营地实际人数不小于v s[last] - s[first-1] >= v 可以得到 s[first-1] - s[last] <= -v;
二:连续营地实际人数不大于营地容量 s[last] - s[first-1] <= total[last] - total[first-1]; // 容易 忽略【such me】
三:每个营地实际人数Ai不超过容量且不小于0 —–> 0 <= s[i] - s[i-1] <= man[i];

问题转化:建图后就是求S[n] - S[0] >= ans即 S[0] - S[n] <= -ans ,我们以n为源点,这里-ans就为 n到0的最短路的相反数 即-dist[0]。

代码

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#define MAXN 1000+10#define MAXM 20000+2000+10 //最多边数 #define INF 1000000000using namespace std;struct Edge{    int from, to, val, next; }edge[MAXM];int head[MAXN], top;int dist[MAXN];//dist[i]:源点到i的最短路 int man[MAXN];//man[i]表示i营地人数容量  int total[MAXN];//表示前i个营地总容量 bool vis[MAXN];int used[MAXN];//判断某点入队次数 int n, m;//n个营地 m个限制 void init(){    top = 0;    for(int i = 0; i <= n; i++)    {        head[i] = -1;        used[i] = 0;        vis[i] = false;    } }void addedge(int u, int v, int w){    Edge E = {u, v, w, head[u]};    edge[top] = E;    head[u] = top++;}bool SPFA(){    for(int i = 0; i <= n; i++)    dist[i] = i==n ? 0 : INF;     queue<int> Q;    Q.push(n);//n为源点     vis[n] = true;    used[n]++;    while(!Q.empty())    {        int u = Q.front();        Q.pop();        vis[u] = false;//去掉标记         for(int i = head[u]; i != -1; i = edge[i].next)        {            Edge E = edge[i];            if(dist[E.to] > dist[u] + E.val)            {                dist[E.to] = dist[u] + E.val;                if(!vis[E.to])                {                    vis[E.to] = true;                    used[E.to]++;                    if(used[E.to] > n)                    return false;//存在负环 无解                     Q.push(E.to);                 }            }        }     }    return true;//找到最短路 } void getMap(){    int first, last, v;//开始营地 结束营地 以及两个营地之间最少人数     /*    我们设第i个营地实际人数为Ai, 前i个营地实际总人数为Si    那么可以得到以下3个关系:    一:第first到第last营地实际人数不小于v s[last] - s[first-1] >= v 可以得到 s[first-1] - s[last] <= -v;    二:连续营地实际人数不大于营地容量 s[last] - s[first-1] <= total[last] - total[first-1];      三:每个营地实际人数Ai不超过容量且不小于0 ----->   0 <= s[i] - s[i-1] <= man[i];      */     man[0] = 0; total[0] = 0;     for(int i = 1; i <= n; i++)    {        scanf("%d", &man[i]);//第i个营地容量         total[i] = man[i];        addedge(i-1, i, man[i]);        addedge(i, i-1, 0);//反向边为0         total[i] += total[i-1];//统计前i个营地容量     }    while(m--)    {        scanf("%d%d%d", &first, &last, &v);//从营地s到营地e最少人数         addedge(last, first-1, -v);        addedge(first-1, last, total[last]-total[first-1]);    }}int main(){    while(scanf("%d%d", &n, &m) != EOF)    {        init();        getMap();        if(SPFA())        printf("%d\n", -dist[0]);        else        printf("Bad Estimations\n");    }    return 0;} 
原创粉丝点击