其他分享
首页 > 其他分享> > 「网络流 24 题」航空路线问题

「网络流 24 题」航空路线问题

作者:互联网

大意: 从西到东依次给出$n$个城市, 给定$m$条双向边, 求一条航空路线, 满足从最东走向最西再回到最东, 且除了起点外其他每个点最多走一次, 且途径城市数最大.

 

等价于求两条从$1$到$n$不交叉的路径, 要求经过点数最大.

建图跑mfmc即可, 最后dfs两次输出路径.

#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;
}

 

标签:24,return,航空,int,ll,路线,INF,include,define
来源: https://www.cnblogs.com/uid001/p/10991838.html