POJ-1716 Integer Intervals

题目大意:

给你n个区间[a, b],问你是否存在一个集合Z,使得|Z∩[a,b]| >= 2

解题思路:

同poj 1201,简化版

POJ-1201 Intervals

代码:

#include <queue>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn = 10000 + 5;
const int INF = 0x3f3f3f3f;

typedef struct node{
    int to, w;
    node(int a = 0, int b = 0){
        to = a; w = b;
    }
}Edge;

vector<Edge> vec[maxn];
int dis[maxn], vis[maxn];

int spfa(int n){
    Edge v;
    int len, p;
    queue<int> q;
    while(!q.empty()) q.pop();
    for(int i = 0; i <= n; ++i){
        vis[i] = 0;
        dis[i] = INF;
    }
    dis[0] = 0;
    vis[0] = 1;
    q.push(0);

    while(!q.empty()){
        p = q.front(); q.pop();
        len = vec[p].size();
        for(int i = 0; i < len; ++i){
            v = vec[p][i];
            if(dis[v.to] > dis[p] + v.w){
                dis[v.to] = dis[p] + v.w;
                if(!vis[v.to]){
                    vis[v.to] = 1;
                    q.push(v.to);
                }
            }
        }
        vis[p] = 0;
    }

    return -dis[n];
}
int main(){
    int n, a, b;
    while(~scanf("%d", &n)){
        int e = 0;
        for(int i = 0; i < maxn; ++i) vec[i].clear();
        for(int i = 0; i < n; ++i){
            scanf("%d%d", &a, &b);
            e = (e < b ? b : e);
            vec[a].push_back(Edge(b+1, -2));
        }
        for(int i = 0; i <= e; ++i){
            vec[i].push_back(Edge(i+1, 0));
            vec[i+1].push_back(Edge(i, 1));
        }
        printf("%d\n", spfa(e + 1));
    }
    return 0;
}


posted @ 2016-08-03 10:39  _Wilbert  阅读(87)  评论(0编辑  收藏  举报