大意:刘备将营地连在了一起,陆逊想要估计出多少人,所以就侦查到了没个营地的容量Ci,即最多有多少士兵,又估计了一下从i营地到j营地最少有多少士兵,求总共最少有多少人,或者估计有误(出现了正、负环)。

 

思路:建模。

s[j]-s[j-1] >= k; 

s[i]-s[i-1] >= 0;

s[i]-s[i-1] <=Ci;

 

CODE:

 

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int SIZE = 10010;
const int INF = 0x3f3f3f3f;
int u[5*SIZE], w[5*SIZE], v[5*SIZE], next[5*SIZE];
int first[SIZE], d[SIZE];
int n, m, cnt;
int sum[SIZE];
int C[SIZE];

void read_graph(int u1, int v1, int w1)
{
    u[cnt] = u1, v[cnt] = v1, w[cnt] = w1;
    next[cnt] = first[u[cnt]];
    first[u[cnt]] = cnt++;
}

void spfa(int src)
{
    int stack[SIZE] = {0};
    bool ins[SIZE] = {0};
    int top = 0;
    for(int i = 0; i <= n; i++) d[i] = (i == src)? 0:-INF;
    stack[top++] = src;
    while(top)
    {
        int x = stack[--top];
        ins[x] = 0;
        for(int e = first[x]; e!=-1; e = next[e]) if(d[v[e]] < d[x]+w[e])
        {
            d[v[e]] = d[x] + w[e];
            if(!ins[v[e]])
            {
                ins[v[e]] = 1;
                if(++sum[v[e]] >= n)
                {
                    printf("Bad Estimations\n");
                    return ;
                }
                stack[top++] = v[e];
            }
        }
    }
    printf("%d\n", d[n]);
}

void init()
{
    memset(first, -1sizeof(first));
    memset(sum, 0sizeof(sum));
    memset(C, 0sizeof(C));
    cnt = 0;
}

int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        init();
        int ci;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &ci);
            read_graph(i-1, i, 0);
            read_graph(i, i-1, -ci);
        }
        while(m--)
        {
            int u1, v1, w1;
            scanf("%d%d%d", &u1, &v1, &w1);
            read_graph(u1-1, v1, w1);
        }
        spfa(0);
    }
    return 0;
}

 

posted on 2012-09-16 21:17  有间博客  阅读(173)  评论(0编辑  收藏  举报