UVa #753 A Plug for UNIX (例题11-7)

86 篇文章 0 订阅

网络最大流问题。


建图的时候,取一个起点s,与所有设备的接口连接;再取一个终点t,与所有插线板的接口连接,流量都设为1。注意这里可能在相同的点之间多次添加边(比如不同的设备有着相同的接口),不过不影响解,只不过可能会增加增广的次数,对效率有所影响。

之后在转换器可以转换的接口之间连线,流量无限大。


这样,求s到t之间的最大流,再用设备数量减去最大流的数量就是答案了。


Floyd算法在这里不是很有必要。求最大流的时候,EdmondsKarp算法(bfs)就可以根据转换器的路径自动寻路了,不需要事先求出传递闭包。如果用Floyd预处理,程序速度要慢不少(0.025s - 0.085s)。


Run Time: 0.025s

#define UVa  "LT11-7.753.cpp"		//A Plug for UNIX
char fileIn[30] = UVa, fileOut[30] = UVa;

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<iostream>
#include<map>
#include<queue>

using namespace std;

struct Adaptor {
    int from, to;
    Adaptor(int a, int b):from(a), to(b){}
};

//Global Variables. Reset upon Each Case!
const int maxtype = 500, INF = 1<<30;
int T, n, m, k, typecnt;
vector<int> devices, receptacles;
vector<Adaptor> adaptors;
map<string, int> dict;
int dist[maxtype][maxtype];
/

struct Edge {
    int from, to, cap, flow;
    Edge(int a, int b, int c):from(a), to(b), cap(c), flow(0){ }
};


struct EdmondsKarp {
    vector<Edge> edges;
    vector<int> G[maxtype];
    int p[maxtype];
    int a[maxtype];

    void init() {
        edges.clear();
        for(int i = 0; i < maxtype; i ++) G[i].clear();
        memset(p, 0, sizeof(p));
    }
    void addEdge(int from, int to, int cap) {
        edges.push_back(Edge(from, to, cap));
        edges.push_back(Edge(to, from, 0));
        G[from].push_back(edges.size()-2);
        G[to].push_back(edges.size()-1);
    }

    int maxFlow(int src, int targ) {
        int flow = 0;
        for(;;) {
            memset(a, 0, sizeof(a));
            a[src] = INF;
            queue<int> q;
            q.push(src);

            while(!q.empty()) {
                int x = q.front(); q.pop();
                for(int i = 0; i < G[x].size(); i ++) {
                    Edge e = edges[G[x][i]];
                    int y = e.to;
                    if(a[y] == 0 && e.cap > e.flow) {
                        a[y] = min(a[x], e.cap - e.flow);
                        q.push(y);
                        p[y] = G[x][i];
                    }
                }
                if(a[targ]) break;
            }
            if(!a[targ]) break;
            for(int u = targ; u != src; u = edges[p[u]].from) {
                edges[p[u]].flow += a[targ];
                edges[p[u]^1].flow -= a[targ];
            }

            flow += a[targ];
        }
        return flow;
    }

};


void init();
int getTypeID(string& arg);
void floyd();

void solve() {
    EdmondsKarp EK;
    EK.init();
    int src = typecnt+1;
    int targ = typecnt+2;
    for(int i = 0; i < devices.size(); i ++) {
        EK.addEdge(src, devices[i], 1);
    }
    for(int i = 0; i < receptacles.size(); i ++) {
        EK.addEdge(receptacles[i], targ, 1);
    }
    for(int i = 0; i < typecnt; i ++) {
        for(int j = 0; j < typecnt; j ++) {
            if(dist[i][j]) EK.addEdge(i, j, INF);
        }
    }

    cout<<m - EK.maxFlow(src, targ)<<endl;
}

int main() {
    cin>>T;
    while(T--) {
        init();
        floyd();
        solve();
        if(T) cout<<endl;
    }
    return 0;
}

void init() {
    typecnt = 0;
    devices.clear();
    receptacles.clear();
    adaptors.clear();
    dict.clear();

    string s1, s2;
    cin>>n;
    for(int i = 0; i < n; i ++) {
        cin>>s1;
        receptacles.push_back(getTypeID(s1));
    }
    cin>>m;
    for(int i = 0; i < m; i ++) {
        cin>>s1>>s2;
        devices.push_back(getTypeID(s2));
    }
    cin>>k;
    for(int i = 0; i < k; i ++) {
        cin>>s1>>s2;
        adaptors.push_back(Adaptor(getTypeID(s1), getTypeID(s2)));
    }
}

int getTypeID(string& arg) {
    if(dict.count(arg)) return dict[arg];
    return dict[arg] = typecnt ++;
}

void floyd() {
    memset(dist, 0, sizeof(dist));
    for(int i = 0; i < typecnt; i ++) dist[i][i] = 1;
    for(int i = 0; i < adaptors.size(); i ++) {
        int x = adaptors[i].from, y = adaptors[i].to;
        dist[x][y] = 1;
    }
//    for(int p = 0; p < typecnt; p ++) {
//        for(int i = 0; i < typecnt; i ++) {
//            for(int j = 0; j < typecnt; j ++) {
//                if(dist[i][p] && dist[p][j])
//                    dist[i][j] = 1;
//            }
//        }
//    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值