zoukankan      html  css  js  c++  java
  • Burn the Linked Camp(bellman 差分约束系统)

    Burn the Linked Camp

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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.

    Input:

    There are multiple test cases! On the first line of each test case, there are two integers n (0<n<=1,000) and m (0<=m<=10,000). On the second line, there are n integers C1��Cn. Then m lines follow, each line has three integers i, j, k (0<i<=j<=n, 0<=k<2^31), meaning that the total number of soldiers from the ith camp to the jth camp is at least k.

    Output:

    For each test case, output one integer in a single line: the least number of all soldiers in Liu Bei's army from Lu Xun's observation. However, Lu Xun's estimations given in the input data may be very unprecise. If his estimations cannot be true, output "Bad Estimations" in a single line instead.

    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

    注意dij只能处理非负权值的最短路问题, 所以差分约束系统一律用 bellman()
    注意:本题应该是用spfa做的,不然bellman O(nm)算法很容易超时, 但此题不但没有超时,竟然能在100ms的时间内通过。


     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <string>
     7 #include <vector>
     8 #include <set>
     9 #include <map>
    10 #include <queue>
    11 #include <stack>
    12 #include <sstream>
    13 #include <iomanip>
    14 using namespace std;
    15 const int INF=0x4fffffff;
    16 const int EXP=1e-6;
    17 const int MS=1005;
    18 
    19 struct edge
    20 {
    21       int u,v,w;
    22 }edges[40*MS];
    23 
    24 int n,m,esize;
    25 int dis[MS];
    26 int C[MS];
    27 int maxv[MS];
    28 
    29 int input()
    30 {
    31       if(scanf("%d%d",&n,&m)==EOF)
    32             return 0;
    33    /*
    34                   c[i]>=s[i]-s[i-1]>=0;
    35       maxv[v]-maxv[u-1]>=s[v]-s[u-1]>=w;
    36       四个约束条件
    37       */
    38       //因为  s[n]-s[i]>=0;   ==>    s[i]<=s[n]+0,   n->i     w=0;
    39       //   所以dis初始化为  0;
    40       memset(dis,0,sizeof(dis));
    41       memset(maxv,0,sizeof(maxv));
    42       esize=0;
    43       for(int i=1;i<=n;i++)
    44       {
    45             scanf("%d",&C[i]);
    46             maxv[i]=C[i]+maxv[i-1];
    47             edges[esize].u=i-1;
    48             edges[esize].v=i;
    49             edges[esize++].w=C[i];
    50 
    51             edges[esize].u=i;
    52             edges[esize].v=i-1;
    53             edges[esize++].w=0;
    54       }
    55       int u,v,w;
    56       for(int i=0;i<m;i++)
    57       {
    58             scanf("%d%d%d",&u,&v,&w);
    59             edges[esize].u=v;
    60             edges[esize].v=u-1;
    61             edges[esize++].w=-w;
    62 
    63             edges[esize].u=u-1;
    64             edges[esize].v=v;
    65             edges[esize++].w=maxv[v]-maxv[u-1];
    66       }
    67       return 1;
    68 }
    69 
    70 bool bellman()
    71 {
    72       for(int i=0;i<=n;i++)      //千万注意定点个数是n+1,不然会错。
    73       {
    74             for(int j=0;j<esize;j++)
    75             {
    76                   if(dis[edges[j].u]+edges[j].w<dis[edges[j].v])
    77                   {
    78                         dis[edges[j].v]=dis[edges[j].u]+edges[j].w;
    79                         if(i==n)
    80                               return false;
    81                   }
    82             }
    83       }
    84       return true;
    85 }
    86 
    87 int main()
    88 {
    89       while(input())
    90       {
    91             if(bellman())
    92                   printf("%d
    ",dis[n]-dis[0]);
    93             else
    94                   printf("Bad Estimations
    ");
    95       }
    96       return 0;
    97 }






  • 相关阅读:
    《白帽子讲web安全》学习笔记(第一篇)
    pytorch安装配置(VScode +miniconda+pytorch)
    tensorflow-GPU安装配置(VScode +miniconda+tensorflow-gpu)
    Win10+VScode +miniconda安装tensorflow(CPU版本)
    The TensorFlow library wasn't compiled to use SSE instructions解决方法
    python安装配置(miniconda版)(Win10+VScode +miniconda)
    Git使用方法
    PDF快速导出为word(免费方法)
    CAJ文件转PDF
    拖拽的效果 第一步 设置 可拖拽的属性 draggable="true" 绑定drag 事件 第二步 设置 放置位置 触发的事件 dragover 第三步 设置 放置之后 触发的事件 dragover 下面请看代码:
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4374785.html
Copyright © 2011-2022 走看看