线性规划与网络流24题 11航空路线问题

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
#define inf 1000000000
#define M 150
#define N 10000
#define cc(m,v) memset(m,v,sizeof(m))
struct node{
int v,f,c,next;
}edge[N];
int que[N];
string str[M];
map<string,int> maze;
void ainit(){
}
void addedge(int u,int v,int f,int c){
}
bool spfa(int s,int t,int n){
int i,u,v,qin=0,qout=0;
for(i=0;i<=n;i++) dis[i]=inf,vis[i]=0;
dis[que[qin++]=s]=0,pre[s]=pre[t]=-1;
while(qout!=qin){
u=que[qout++],vis[u]=0;
if(qout==N) qout=0;
if(edge[i].f>0 && dis[u]+edge[i].c<dis[v=edge[i].v]){
dis[v]=dis[u]+edge[i].c;
pre[v]=i^1;
if(!vis[v]){
vis[v]=1,que[qin++]=v;
if(qin==N) qin=0;
}
}
}
return pre[t]>=0;
}
int spfaflow(int s,int t,int n){
int i,flow=0,f;
ans=0;
while(spfa(s,t,n)){
for(i=pre[t],f=inf;i>=0;i=pre[edge[i].v])
if(edge[i^1].f<f) f=edge[i^1].f;
for(i=pre[t];i>=0;i=pre[edge[i].v])
edge[i].f+=f,edge[i^1].f-=f;
ans-=f*dis[t],flow+=f;
}
return flow;
}
void prin(int u,int n){
vis[u]=1;
if(!vis[v=edge[i].v] && ((!edge[i].f && edge[i].c<=0)||(edge[i].f&&edge[i].c>=0))){
prin(v,n);
if(v<=n)cout<<str[v]<<endl;
}
}
int main(){
int n,m,i;
string str1,str2;
while(scanf("%d%d",&n,&m)!=-1){
ainit();
maze.clear();
for(i=1;i<=n;i++){
cin>>str[i];
maze[str[i]]=i;
}
for(i=1;i<=m;i++){
cin>>str1>>str2;
int a=maze[str1],b=maze[str2];
if(a<b){
}else{
}
}
if(spfaflow(1,2*n,2*n+1)==2){
printf("%d\n",ans);
cout<<str[1]<<endl;
prin(1,n);
cout<<str[1]<<endl;
}else printf("No Solution!\n");
}
}