"24 network flow problem" Air Routes

Effect: from west to east in turn gives $ n $ cities, two-way given $ m $ edge strip, seeking a air routes, the most satisfaction from east to west and then back to the East most, and in addition to the start point of each other most go once, and the maximum number of ways the city.

 

Is equivalent to finding two paths from $ 1 to $ $ $ n-do not intersect, the maximum number of points required through.

Figure mfmc built to run the last two dfs output path.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#include <unordered_map>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head


const int N = 1e6+10;
int n, m, S, T;
struct _ {int from,to,w,f;};
vector<_> E;
vector<int> g[N];
int a[N], pre[N], inq[N], d[N];
int mf,mc;
queue<int> q;
void add(int x, int y, int c, int w) {
    g[x].pb(E.size());
    E.pb({x,y,c,w});
    g[y].pb(E.size());
    E.pb({y,x,0,-w});
}
void mfmc() {
    mf=mc=0;
    while (1) {
        REP(i,1,T) a[i]=d[i]=INF,inq[i]=0;
        q.push(S),d[S]=0;
        while (!q.empty()) {
            int x=q.front(); q.pop();
            inq[x] = 0;
            for (auto t:g[x]) {
                auto e=E[t];
                if (e.w>0&&d[e.to]>d[x]+e.f) {
                    d[e.to]=d[x]+e.f;
                    pre[e.to]=t;
                    a[e.to]=min(a[x],e.w);
                    if (!inq[e.to]) {
                        inq[e.to]=1;
                        q.push(e.to);
                    }
                }
            }
        }
        if (a[T]==INF) break;
        for (int u=T;u!=S;u=E[pre[u]].from) {
            E[pre[u]].w-=a[T];
            E[pre[u]^1].w+=a[T];
        }
        mf+=a[T],mc+=a[T]*d[T];
    }
}

map<string,int> f;
string val[N];
int ID(string s) {
	if (f.count(s)) return f[s];
	int t = f.size()+1;
	val[t] = s;
	return f[s] = t;
}

vector<string> v1, v2;

void dfs(int x, vector<string> &v) {
	if (1<=x&&x<=n) v.pb(val[x]);
	for (auto t:g[x]) {
		if (t%2==0&&E[t^1].w) { 
			--E[t^1].w;
			return dfs(E[t].to,v);
		}
	}
}

int main() {
	scanf("%d%d", &n, &m);
	REP(i,1,n) {
		string s;
		cin>>s,ID(s);
	}
	REP(i,1,m) {
		string x, y;
		cin>>x>>y;
		add(ID(x)+n,ID(y),INF,0);
	}
	REP(i,1,n) add(i,i+n,i==1||i==n?2:1,-1);
	S = 2*n+1, T = S+1;
	add(S,1,INF,0),add(n,T,INF,0);
	mfmc();
	if (mf!=2) return puts("No Solution!"),0;
	printf("%d\n", -mc);
	dfs(1,v1),dfs(1,v2);	
	v2.pop_back();
	for (auto t:v1) cout<<t<<endl;
	for (auto t=v2.rbegin(); t!=v2.rend(); ++t) cout<<*t<<endl;
}

 

Guess you like

Origin www.cnblogs.com/uid001/p/10991838.html