码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 2770 Burn the Linked Camp

时间:2015-05-20 23:47:27      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

今天刚刚学差分约束系统。利用最短路求解不等式。世界真的好奇妙!感觉不等式漏下几个会导致WA!!

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn = 1111;
vector<int>ljb[maxn];//邻接表
int jz[maxn][maxn];//邻接矩阵
int n, m, shibai;
int c[maxn], d[maxn];
int dist[maxn];
int flag[maxn], tott[maxn];

void spfa()
{
    int ii;
    queue<int>Q;
    memset(flag, 0, sizeof(flag));
    memset(tott, 0, sizeof(tott));
    flag[n] = 1; tott[n]++;
    for (ii = 0; ii <= n; ii++) dist[ii] = 999999999;
    dist[n] = 0; Q.push(n);
    while (!Q.empty())
    {
        int hh = Q.front(); Q.pop(); flag[hh] = 0;
        for (ii = 0; ii < ljb[hh].size(); ii++)
        {
            if (jz[hh][ljb[hh][ii]] != 999999999)
            {
                if (dist[hh] + jz[hh][ljb[hh][ii]] < dist[ljb[hh][ii]])
                {
                    dist[ljb[hh][ii]] = dist[hh] + jz[hh][ljb[hh][ii]];
                    if (flag[ljb[hh][ii]] == 0)
                    {
                        Q.push(ljb[hh][ii]);
                        flag[ljb[hh][ii]] = 1;
                        tott[ljb[hh][ii]]++;
                        if (tott[ljb[hh][ii]] >= n)    { shibai = 1; break; }
                    }
                }
            }
        }
        if (shibai) break;
    }
}

int main()
{
    int i, j, u, v, k;
    while (~scanf("%d%d", &n, &m))
    {
        shibai = 0; c[0] = 0; d[0] = 0;
        for (i = 0; i <= n; i++) ljb[i].clear();
        for (i = 0; i <= n; i++)
        {
            for (j = 0; j <= n; j++)
            {
                if (i == j) jz[i][j] = 0;
                else jz[i][j] = 999999999;
            }
        }
        for (i = 1; i <= n; i++) scanf("%d", &c[i]);
        for (i = 1; i <= n; i++) d[i] = c[i] + d[i - 1];
        for (i = 0; i < m; i++)
        {
            scanf("%d%d%d", &v, &u, &k);
            jz[u][v - 1] = -k;
            ljb[u].push_back(v - 1);
            jz[v - 1][u] = d[u] - d[v - 1];
            ljb[v - 1].push_back(u);
        }
        for (i = 1; i <= n; i++)
        {
            jz[i - 1][i] = c[i];
            ljb[i - 1].push_back(i);
            jz[i][i - 1] = 0;
            ljb[i].push_back(i - 1);
        }
        spfa();
        if (shibai == 1) printf("Bad Estimations\n");
        else printf("%d\n", -dist[0]);
    }
    return 0;
}

 

zoj 2770 Burn the Linked Camp

标签:

原文地址:http://www.cnblogs.com/zufezzt/p/4518305.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!