zoj2770

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

 

 

#include <cstdio>
#define INF 9999999
#define NMAX 1001
#define EMAX 23000

int n;            //一共有n个大营
int m;            //已知从第i个大营到第j个大营至少有多少士兵,这些信息有m条
int c[NMAX];    //第i个大营最多有c[i]个士兵
int dist[NMAX];    //从源点到各个顶点最短路径长度(注意:源点为Sn而不是S0)
int d[NMAX];//d[0]=0,d[1]=c[1],d[2]=c[1]+c[2],...,即d[i]为前i个大营容量总和
int ei;            //边的序号

struct eg
{
    int u, v, w;    //边:起点、终点、权值
}edges[EMAX];

void init( )    //初始化函数
{
    ei = 0;
    int i;
    //除源点Sn外,其他顶点的最短距离初始为INF,
    //则bellman-ford算法的第1重循环要执行n-1次
    for( i=0; i<=n; i++ )  dist[i] = INF;
    d[0] = 0;
    dist[n] = 0;    //以下bellman-ford算法是以Sn为源点的,所以dist[n]为0
}

//如果存在源点可达的带负权值边的回路,则返回false
bool bellman_ford( )    //bellman-ford算法
{
    int i, k, t;
    //bellman-ford算法的第1重循环要执行N-1次,N是网络中顶点个数
    //在本题中,顶点个数是n+1
    for( i=0; i<n; i++ )
    {
        //假设第k条边的起点是u,终点是v,以下循环考虑第k条边是否会使得源点v0到v的
        //最短距离缩短,即判断dist[edges[k].u] + edges[k].w < dist[edges[k].v]
        //是否成立
        for(k = 0; k < ei; k ++)
        {
            t = dist[edges[k].u] + edges[k].w;
            if( dist[edges[k].u] != INF && t < dist[edges[k].v] )
                dist[edges[k].v] = t;
        }
    }
    //以下是检查,若还有更新则说明存在无限循环的负值回路
    for(k = 0; k < ei; k ++)
    {
        if( dist[edges[k].u] != INF &&
            dist[edges[k].u] + edges[k].w < dist[edges[k].v] )
            return false;
    }
    return true;
}

int main( )
{
    while( scanf("%d %d", &n, &m) != EOF )//输入数据一直到文件尾
    {
        init( );
        int i, u, v, w;
        for( i=1; i<=n; i++ )    //构造不等式组
        {
            scanf("%d", &c[i]);    //读入第i个兵营最多有ci个士兵
            edges[ei].u = i - 1;  edges[ei].v = i;
            edges[ei].w = c[i];    //构造边<i-1,i>, 权值为Ci
            ei++;
            edges[ei].u = i;  edges[ei].v = i - 1;
            edges[ei].w = 0;        //构造边<i,i-1>, 权值为0
            ei++;
            d[i] = c[i] + d[i - 1];
        }
        for( i = 0; i < m; i ++ )    //构造不等式组
        {
            scanf("%d %d %d", &u, &v, &w);
            edges[ei].u = v;  edges[ei].v = u - 1;
            edges[ei].w = -w;    //构造边<v,u-1>,权值为-w
            ei++;
            edges[ei].u = u - 1;  edges[ei].v = v;
            edges[ei].w = d[v] - d[u - 1];    //构造边<u-1,v>,权值为d[v] - d[u - 1]
            ei++;
        }
        if( !bellman_ford( ) )  printf("Bad Estimations\n");
        else  printf("%d\n", dist[n] - dist[0]);
    }
    return 0;
}

 

 用无敌算法SPFA:

犯了一个小小的错误 无语啊 找了那么久....  

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
using namespace std;
const int inf = 1000000;
const int maxn = 23000;
int dist[maxn],head[maxn],sum[maxn] ,n,m,num,a[maxn];
bool flag[maxn];
struct Node
{
    int u,v,w,next;
}node[maxn];
void add(int u,int v,int w)
{
    node[num].u = u;
    node[num].v = v;
    node[num].w = w;
    node[num].next = head[u];
    head[u] = num++;
}
int SPFA(int s)
{
    int i;
    for(i=0;i<=n;i++)
    { 
        sum[i] = 0;
        dist[i] = inf;
        flag[i] = false;
    }
    queue<int>q;
    q.push(s);
    ++sum[s];
    flag[s] = true;
    dist[s] = 0;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        flag[u] = false;
        for(i=head[u];~i;i=node[i].next)
        {
            int v = node[i].v;
            int w = node[i].w;
            if(dist[v]>dist[u]+w)
            {
                dist[v] = dist[u] + w;
                if(!flag[v])
                {
                    q.push(v);
                    flag[v]  =true;
                    sum[v]++;
                    if(sum[v]>n)
                        return 0;
                }
            }
        }
    }
    if(dist[n]==inf)
    {
        return 0;
    }
    else
    {

        return 1;
    }
    
}
int main()
{
    int from,to,man,i,j;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        num = 0;
        memset(head,-1,sizeof(head));
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            add(i-1,i,a[i]);
            add(i,i-1,0);
        }
        for(i=1;i<=m;i++)
        {
            scanf("%d %d %d",&from,&to,&man);
            add(to,from-1,-man);
            int sum = 0;
            for(j=from;j<=to;j++)
            {
                sum+=a[j];
            }
            add(from-1,to,sum);
        }
        
        int temp;
        temp = SPFA(n);
        
        
        if(temp)
            printf("%d\n",-dist[0]);
        else
            printf("Bad Estimations\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Deng1185246160/p/3236460.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值