加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

POJ PKU 1716 查分约束

(2010-08-04 16:30:51)
标签:

poj

pku

1716

查分约束

it

分类: 图论

题目描述:http://acm.pku.edu.cn/JudgeOnline/problem?id=1716

解题报告:http://blog.sina.com.cn/s/blog_64675f540100jri9.html

代码如下:(bellman超时,改成了spfa)

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n, d[10005], cnt, a, b, mmax, v[10005], que[10000*50];
struct edge{int to, v, next;} e[70000];
void insert(int from, int to, int va)
{
    e[cnt].to = to; e[cnt].v = va;
    e[cnt].next = v[from];//每次都插入到最前面
    v[from] = cnt++;
}
bool vst[10005];
void spfa()
{
    memset(vst, 0, sizeof(vst));
    memset(d, -1, sizeof(d));
    d[mmax + 1] = 0;
    int head = 0, tail = 0;
    que[tail++] = mmax + 1;vst[mmax + 1] = 1;
    while(head != tail)
    {
        int from = que[head], id = v[que[head]];
        vst[from] = 0;
        head++;
        while(id != -1)
        {
            int to = e[id].to;
            if (d[to] == -1 || d[from] + e[id].v < d[to])
            {
                d[to] = d[from] + e[id].v;
                if (!vst[to])
                {
                    vst[to] = 1;
                    que[tail++] = to;
                }
            }
            id = e[id].next;
        }
    }
}
int main()
{
    scanf("%d", &n); cnt = mmax = 0;
    memset(v, -1, sizeof(v));
    for(int i = 0; i < n; i++)
    {
        scanf("%d%d", &a, &b);
        insert(b + 1, a, -2);
        if (a > mmax) mmax = a; if (b + 1 > mmax) mmax = b + 1;
    }
    for(int i = 0; i < mmax; i++) insert(i, i + 1, 1);
    for(int i = 1; i <= mmax; i++) insert(i, i - 1, 0);
    for(int i = 0; i <= mmax; i++) insert(mmax + 1, i, 0);
    spfa();
    printf("%d\n", d[mmax] - d[0]);
    return 0;
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有