• luogu2756 飞行员配对方案问题


    匈牙利

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    int n, m, lst[105], uu, vv, cnt, ans;
    bool edge[105][105], vis[105];
    bool dfs(int x){
    	for(int i=m+1; i<=n; i++)
    		if(edge[x][i] && !vis[i]){
    			vis[i] = true;
    			if(!lst[i] || dfs(lst[i])){
    				lst[i] = x;
    				return true;
    			}
    		}
    	return false;
    }
    int main(){
    	cin>>m>>n;
    	while(scanf("%d %d", &uu, &vv)!=EOF && uu>0)
    		edge[uu][vv] = true;
    	for(int i=1; i<=m; i++){
    		memset(vis, 0, sizeof(vis));
    		if(dfs(i))	ans++;
    	}
    	if(!ans)	cout<<"No Solution!"<<endl;
    	else cout<<ans<<endl;
    	for(int i=m+1; i<=n; i++)
    		if(lst[i])
    			printf("%d %d
    ", lst[i], i);
    	return 0;
    }
    

    dinic

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <queue>
    using namespace std;
    struct Edge{
    	int too, nxt, val;
    }edge[10005];
    int m, n, hea[105], uu, vv, cnt, lev[105], ss, tt, maxFlow;
    const int oo=0x3f3f3f3f;
    bool a[105][105];
    queue<int> d;
    void add_edge(int fro, int too, int val){
    	edge[cnt].nxt = hea[fro];
    	edge[cnt].too = too;
    	edge[cnt].val = val;
    	hea[fro] = cnt++;
    }
    void addEdge(int fro, int too, int val){
    	add_edge(fro, too, val);
    	add_edge(too, fro, 0);
    }
    bool bfs(){
    	memset(lev, 0, sizeof(lev));
    	d.push(ss);
    	lev[ss] = 1;
    	while(!d.empty()){
    		int x=d.front();
    		d.pop();
    		for(int i=hea[x]; i!=-1; i=edge[i].nxt){
    			int t=edge[i].too;
    			if(!lev[t] && edge[i].val>0){
    				lev[t] = lev[x] + 1;
    				d.push(t);
    			}
    		}
    	}
    	return lev[tt]!=0;
    }
    int dfs(int x, int lim){
    	if(x==tt)	return lim;
    	int addFlow=0;
    	for(int i=hea[x]; i!=-1 && addFlow<lim; i=edge[i].nxt){
    		int t=edge[i].too;
    		if(lev[t]==lev[x]+1 && edge[i].val>0){
    			int tmp=dfs(t, min(lim-addFlow, edge[i].val));
    			edge[i].val -= tmp;
    			edge[i^1].val += tmp;
    			addFlow += tmp;
    		}
    	}
    	return addFlow;
    }
    void dinic(){
    	while(bfs())	maxFlow += dfs(ss, oo);
    }
    int main(){
    	memset(hea, -1, sizeof(hea));
    	cin>>m>>n;
    	ss = 0; tt = n + 1;
    	while(scanf("%d %d", &uu, &vv)!=EOF && uu>0)
    		a[uu][vv] = true;
    	for(int i=1; i<=m; i++){
    		addEdge(ss, i, 1);
    		for(int j=m+1; j<=n; j++)
    			if(a[i][j])
    				addEdge(i, j, 1);
    	}
    	for(int i=m+1; i<=n; i++)
    		addEdge(i, tt, 1);
    	dinic();
    	if(!maxFlow)	cout<<"No Solution!
    ";
    	else cout<<maxFlow<<endl;
    	for(int i=1; i<=m; i++)
    		for(int j=hea[i]; j!=-1; j=edge[j].nxt)
    			if(edge[j^1].val && edge[j].too)
    				printf("%d %d
    ", i, edge[j].too);
    	return 0;
    }
    
  • 相关阅读:
    Android源码之Gallery专题研究(2)
    Android源码之Gallery专题研究(1)
    Android UI 优化——使用HierarchyViewer工具
    如何打开USB OTG功能:
    JavaSE入门学习5:Java基础语法之keyword,标识符,凝视,常量和变量
    Multiply Strings
    android动态控制组件的位置、大小和新的动画
    linux系统编程:线程同步-信号量(semaphore)
    imx6q GPIO功能的用法
    NoSQL数据库概览及其与SQL语法的比較
  • 原文地址:https://www.cnblogs.com/poorpool/p/8110842.html
Copyright © 2020-2023  润新知