POJ 1637 -- Sightseeing tour

代码实现:

#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
#include<vector>
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define Abs(a) ((a)>(0)?(a):(-a))
using namespace std;
const int N=210, M=2810, INF=0x3f3f3f3f;
int n, m, s, t, top;
int head[N], gap[N], cur[N], dis[N], vis[N], pre[N], in[N], out[N];

struct Edge{
    int to, next, flow;
    Edge(int _to = 0, int _next = 0, int _flow = 0):to(_to), next(_next), flow(_flow){}
}edge[M];

void Addedge(int from, int to, int flow){
    edge[top] = Edge(to, head[from], flow);
    head[from] = top++;
    edge[top] = Edge(from, head[to], 0);
    head[to] = top++;
}

void Bfs(){
    queue<int> q;
    memset(gap, 0, sizeof(gap));
    memset(dis, -1, sizeof(dis));
    gap[0] = 1; dis[t] = 0; q.push(t);
    while(!q.empty()){
        int u = q.front(); q.pop();
        for(int i = head[u]; i+1; i = edge[i].next){
            if(dis[edge[i].to] == -1){
                dis[edge[i].to] = dis[u] + 1;
                gap[dis[edge[i].to]] ++;
                q.push(edge[i].to);
            }
        }
    }
}

int Sap(){
    Bfs();
    memset(pre, -1, sizeof(pre));
    for(int i = s; i <= t; ++i) cur[i] = head[i];
    int u = s, cur_flow, max_flow = 0, neck, tmp;
    while(dis[s] <= t){
        if(u == t){
            cur_flow = INF;
            for(int i = s; i != t; i = edge[cur[i]].to){
                if(cur_flow > edge[cur[i]].flow){
                    cur_flow = edge[cur[i]].flow;
                    neck = i;
                }
            }
            for(int i = s; i != t; i = edge[cur[i]].to){
                tmp = cur[i];
                edge[tmp].flow -= cur_flow;
                edge[tmp^1].flow += cur_flow;
            }
            max_flow += cur_flow;
            u = neck;
        }
        int i;
        for(i = cur[u]; i + 1; i = edge[i].next){
            if(edge[i].flow && dis[u] == dis[edge[i].to] + 1) break;
        }
        if(i != -1){
            cur[u] = i;
            pre[edge[i].to] = u;
            u = edge[i].to;
        }else{
            if(--gap[dis[u]] == 0) break;
            cur[u] = head[u];
            int mindis = t;
            for(i = head[u]; i + 1; i = edge[i].next)
                if(edge[i].flow && mindis > dis[edge[i].to]) mindis = dis[edge[i].to];
            dis[u] = mindis + 1;
            gap[dis[u]] ++;
            if(u != s) u = pre[u];
        }
    }
    return max_flow;
}

int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        scanf("%d%d", &n, &m);
        memset(head, -1, sizeof(head));
        memset(in, 0, sizeof(in));
        memset(out, 0, sizeof(out));
        top = s = 0; t = n + 1;
        int u, v, d, sum = 0, flag = 0;
        for(int i = 1; i <= m; ++i){
            scanf("%d%d%d", &u, &v, &d);
            in[v]++, out[u]++;
            if(d == 0) Addedge(u, v, 1);
        }
        for(int i = 1; i <= n; ++i){
            if(in[i] > out[i]) Addedge(i, t, (in[i]-out[i])/2);
            else Addedge(s, i, (out[i]-in[i])/2), sum+=((out[i]-in[i])/2);
            if((Abs(in[i]-out[i]))%2) flag = 1;
        }
        if(flag){
            printf("impossible\n");
            continue;
        }
        int res = Sap();
        if(res == sum) printf("possible\n");
        else printf("impossible\n");
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值