UVa 11248 - Frequency Hopping 最大流

题目链接:UVa 11248 - Frequency Hopping

题目大意:给你一个有向图,n个点,m条边,1为源点,n为汇点,另给一需求C,问是否存在流的大小为C。如果存在,输出possible,如果不存在,是否可以通过只修改一条边使得条件成立?如果有解,按弧头为第一关键字,弧尾为第二关键字升序输出;无解则输出not possible

题目分析:先跑一次最大流,如果>=C则直接possible。否则,对所有关键边(容量 = 0(增大流量等于减少容量))容量依次 + C再跑一次最大流,如果条件得到满足则该边为解之一;每一次跑完最大流之后需要对流过的边回溯,这个通过辅助数组记录需要回溯的边就好。关键边的记录也通过一个辅助数组记录。

这次需要对ISAP算法进行略微的修改了,在改变边的容量的时候如果是第一次跑则记录关键边,如果不是则记录需要回溯的边。

PS:总是细节方面问题多多。。。。最后因为忘记对结果排序蛙了一发  +  皿  +。


#include <stdio.h>
#include <string.h>
#include <algorithm>
#define REP(I, X) for(int I = 0; I < X; ++I)
#define FF(I, A, B) for(int I = A; I <= B; ++I)
#define clear(A, B) memset(A, B, sizeof A)
#define copy(A, B) memcpy(A, B, sizeof A)
#define min(A, B) ((A) < (B) ? (A) : (B))
#define max(A, B) ((A) > (B) ? (A) : (B))
using namespace std;
typedef long long ll;
typedef long long LL;
const int oo = 2147483647;
const int maxE = 1000000;
const int maxN = 105;
const int maxQ = 10000;
struct Edge{
    int v, c, n;
};
struct Node{
    int one, two;
};
Edge edge[maxE];
Node ans[maxE], change[maxE];
int adj[maxN], cntE, anscnt, changecnt;
int Q[maxE], head, tail, inq[maxN];
int d[maxN], num[maxN], cur[maxN], pre[maxN];
int s, t, nv;
int n, m, need;
int get[maxE], getcnt;
void addedge(int u, int v, int c){
    edge[cntE].v = v; edge[cntE].c = c; edge[cntE].n = adj[u]; adj[u] = cntE++;
    edge[cntE].v = u; edge[cntE].c = 0; edge[cntE].n = adj[v]; adj[v] = cntE++;
}
Node ANS(int i){
    Node tmp = {edge[i ^ 1].v, edge[i].v};
    return tmp;
}
Node CHANGE(int i, int c){
    Node tmp = {i, c};
    return tmp;
}
int cmp(const Node &a, const Node &b){
    return a.one != b.one ? a.one < b.one : a.two < b.two;
}
void BACK(){
    for(int i = 0; i < changecnt; ++i){
        int ii = change[i].one, c = change[i].two;
        edge[ii].c += c;
        edge[ii ^ 1].c -= c;
    }
}
void REV_BFS(){
    clear(d, -1);
    clear(num, 0);
    head = tail = 0;
    d[t] = 0;
    num[0] = 1;
    Q[tail++] = t;
    while(head != tail){
        int u = Q[head++];
        for(int i = adj[u]; ~i; i = edge[i].n){
            int v = edge[i].v;
            if(~d[v]) continue;
            d[v] = d[u] + 1;
            num[d[v]]++;
            Q[tail++] = v;
        }
    }
}
int ISAP(int ch){
    copy(cur, adj);
    REV_BFS();
    int flow = 0, u = pre[s] = s, i;
    while(d[s] < nv){
        if(u == t){
            int f = oo, neck;
            for(i = s; i != t; i = edge[cur[i]].v){
                if(f > edge[cur[i]].c){
                    f = edge[cur[i]].c;
                    neck = i;
                }
            }
            for(i = s; i != t; i = edge[cur[i]].v){
                edge[cur[i]].c -= f;
                edge[cur[i] ^ 1].c += f;
                if(ch && !edge[cur[i]].c) get[getcnt++] = cur[i];
                else change[changecnt++] = CHANGE(cur[i], f);
            }
            flow += f;
            u = neck;
        }
        for(i = cur[u]; ~i; i = edge[i].n) if(edge[i].c && d[u] == d[edge[i].v] + 1) break;
        if(~i){
            cur[u] = i;
            pre[edge[i].v] = u;
            u = edge[i].v;
        }
        else{
            if(0 == (--num[d[u]])) break;
            int mind = nv;
            for(i = adj[u]; ~i; i = edge[i].n){
                if(edge[i].c && mind > d[edge[i].v]){
                    mind = d[edge[i].v];
                    cur[u] = i;
                }
            }
            d[u] = mind + 1;
            num[d[u]]++;
            u = pre[u];
        }
    }
    return flow;
}
void work(){
    int u, v, c, flow;
    clear(adj, -1);
    cntE = getcnt = anscnt = 0;
    s = 1; t = n; nv = t + 1;
    while(m--){
        scanf("%d%d%d", &u, &v, &c);
        addedge(u, v, c);
    }
    flow = ISAP(1);
    if(flow >= need) printf("possible\n");
    else{
        for(int i = 0; i < getcnt; ++i){
            changecnt = 0;
            edge[get[i]].c += need;
            if(flow + ISAP(0) >= need) ans[anscnt++] = ANS(get[i]);
            BACK();
            edge[get[i]].c = 0;
        }
        if(!anscnt) printf("not possible\n");
        else{
            sort(ans, ans + anscnt, cmp);
            printf("possible option:");
            for(int i = 0; i < anscnt; ++i){
                int u = ans[i].one, v = ans[i].two;
                if(i) printf(",");
                printf("(%d,%d)", u, v);
            }
            printf("\n");
        }
    }
}
int main(){
    int cas = 0;
    while(~scanf("%d%d%d", &n, &m, &need) && (n || m || need)){
        printf("Case %d: ", ++cas);
        work();
    }
    return 0;
}




你可能感兴趣的:(uva)