# 【网络流24题】航空线路问题（费用流）

xiaoxiao2021-02-28  6

Cogs数据有误，提供洛谷题面

## 题解

#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<set> #include<map> #include<vector> #include<queue> using namespace std; #define MAX 5000 #define MAXL 500000 #define INF 1000000000 inline int read() { int x=0,t=1;char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-')ch=getchar(); if(ch=='-')t=-1,ch=getchar(); while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar(); return x*t; } struct Line { int v,next,w,fy; }e[MAXL]; int h[MAX],cnt=2; inline void Add(int u,int v,int w,int fy) { e[cnt]=(Line){v,h[u],w,fy};h[u]=cnt++; e[cnt]=(Line){u,h[v],0,-fy};h[v]=cnt++; } int pe[MAX],pr[MAX],dis[MAX]; bool vis[MAX]; int S,T,Cost,n,m,Flow; bool SPFA() { memset(dis,63,sizeof(dis)); queue<int> Q; Q.push(S);dis[S]=0; while(!Q.empty()) { int u=Q.front();Q.pop(); for(int i=h[u];i;i=e[i].next) { int v=e[i].v; if(e[i].w&&dis[v]>dis[u]+e[i].fy) { dis[v]=dis[u]+e[i].fy; pe[v]=i;pr[v]=u; if(!vis[v])vis[v]=true,Q.push(v); } } vis[u]=false; } if(dis[T]>=INF)return false; int flow=INF; for(int i=T;i!=S;i=pr[i])flow=min(flow,e[pe[i]].w); for(int i=T;i!=S;i=pr[i])e[pe[i]].w-=flow,e[pe[i]^1].w+=flow; Cost-=flow*dis[T]; Flow+=flow; return true; } string name[MAX]; map<string,int> M; vector<int> ans; void output(int u) { ans.push_back(u); if(u==n)return; for(int i=h[u+n];i;i=e[i].next) if(!vis[i]&&e[i^1].w){vis[i]=true;output(e[i].v);return;} } int main() { cin>>n>>m; S=0;T=n+n+1; for(int i=1;i<=n;++i){cin>>name[i];M[name[i]]=i;} for(int i=1;i<=m;++i) { string s1,s2; cin>>s1>>s2; int u=M[s1],v=M[s2]; if(u>v)swap(u,v); Add(u+n,v,INF,0); } Add(S,1,2,0);Add(1,1+n,2,0); Add(n+n,T,2,0);Add(n,n+n,INF,0); for(int i=2;i<n;++i)Add(i,i+n,1,-1); while(SPFA()); if(Flow!=2){puts("No Solution!");return 0;} printf("%d\n",Cost+2); bool fl=false; output(1); for(int i=0;i<ans.size();++i)cout<<name[ans[i]]<<endl; ans.clear();output(1); for(int i=ans.size()-1;i>=0;--i)if(ans[i]!=n)cout<<name[ans[i]]<<endl; return 0; }