求2个不相交的路径,这个算是非常经典的题了吧。。
由于每个点只能经过一次,所以拆点限流,容量和费用均为1,这里费用就可以用来表示经过的点数了。。不过起点和终点的流量可以为2。。
然后2个路径就对应2个流,只要跑最大费用最大流即可。。
中间考虑到了可能会有环的情况,其实并不用考虑,因为路径的方向是单向的。。
不过输出方案就比较烦。。要根据图的特点去把方案输出来。。
这方法或许可以推广到m个不相交路径上?
/** * ┏┓ ┏┓ * ┏┛┗━━━━━━━┛┗━━━┓ * ┃ ┃ * ┃ ━ ┃ * ┃ > < ┃ * ┃ ┃ * ┃... ⌒ ... ┃ * ┃ ┃ * ┗━┓ ┏━┛ * ┃ ┃ Code is far away from bug with the animal protecting * ┃ ┃ 神兽保佑,代码无bug * ┃ ┃ * ┃ ┃ * ┃ ┃ * ┃ ┃ * ┃ ┗━━━┓ * ┃ ┣┓ * ┃ ┏┛ * ┗┓┓┏━┳┓┏┛ * ┃┫┫ ┃┫┫ * ┗┻┛ ┗┻┛ */ #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<set> #define inc(i,l,r) for(int i=l;i<=r;i++) #define dec(i,l,r) for(int i=l;i>=r;i--) #define link(x) for(edge *j=h[x];j;j=j->next) #define mem(a) memset(a,0,sizeof(a)) #define ll long long #define eps 1e-12 #define succ(x) (1<<x) #define lowbit(x) (x&(-x)) #define sqr(x) ((x)*(x)) #define mid (x+y>>1) #define NM 100005 #define nm 500005 #define pi 3.1415926535897931 using namespace std; const int inf=1000000007; ll read(){ ll x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return f*x; } struct edge{int t,v,w;edge*next,*rev;}e[nm],*h[NM],*o=e,*p[NM]; void _add(int x,int y,int w,int v){o->t=y;o->w=w;o->v=v;o->next=h[x];h[x]=o++;} void add(int x,int y,int w,int v){_add(x,y,w,v);_add(y,x,0,-v);h[x]->rev=h[y];h[y]->rev=h[x];} int n,m,_x,_y,d[NM],b[NM],s,c[NM],tot; bool v[NM]; map<string,int>_v; queue<int>q; string _s,st[NM]; int spfa(){ mem(v);mem(b);mem(p);mem(d); b[1]=inf;q.push(1);v[1]++;d[1]=0; while(!q.empty()){ int t=q.front();q.pop();v[t]=false; link(t)if(j->w&&d[j->t]<d[t]+j->v){ d[j->t]=d[t]+j->v;p[j->t]=j;b[j->t]=min(j->w,b[t]); if(!v[j->t])q.push(j->t),v[j->t]++; } } return b[n]; } void dfs(int x){ c[++tot]=x; link(x+n/2)if(!j->w&&j->v>=0){dfs(j->t);j->w=1;break;} } int main(){ n=read();m=read(); inc(i,1,n){cin>>_s;_v[_s]=i;st[i]=_s;} inc(i,2,n-1)add(i,n+i,1,1); add(1,n+1,2,1);add(n,n<<1,2,1); inc(i,1,m){ cin>>_s;_x=_v[_s]; cin>>_s;_y=_v[_s]; if(_x>_y)swap(_x,_y); add(_x+n,_y,1,0); if(_x==1&&_y==n)add(_x+n,_y,1,0); } n=n*2; while(spfa()){ s+=d[n]*b[n]; for(int x=n;p[x];x=p[x]->rev->t)p[x]->w-=b[n],p[x]->rev->w+=b[n]; } //printf("%d\n",s); if(s<=2)return 0*printf("No Solution!\n"); printf("%d\n",s-2); dfs(1); // inc(i,1,tot)printf("%d ",c[i]);putchar('\n'); inc(i,1,tot)cout<<st[c[i]]<<endl; tot=0;dfs(1); dec(i,tot-1,1)cout<<st[c[i]]<<endl; return 0; }
P2770 航空路线问题
题目描述
给定一张航空图,图中顶点代表城市,边代表 2 城市间的直通航线。现要求找出一条满足下述限制条件的且途经城市最多的旅行路线。
(1)从最西端城市出发,单向从西向东途经若干城市到达最东端城市,然后再单向从东向西飞回起点(可途经若干城市)。
(2)除起点城市外,任何城市只能访问 1 次。
对于给定的航空图,试设计一个算法找出一条满足要求的最佳航空旅行路线。
输入输出格式
输入格式:第 1 行有 2 个正整数 N 和 V,N 表示城市数,N<100,V 表示直飞航线数。
接下来的 N 行中每一行是一个城市名,可乘飞机访问这些城市。城市名出现的顺序是从西向东。也就是说,设 i,j 是城市表列中城市出现的顺序,当 i>j 时,表示城市 i 在城市 j 的东边,而且不会有 2 个城市在同一条经线上。城市名是一个长度不超过15 的字符串,串中的字符可以是字母或阿拉伯数字。例如,AGR34 或 BEL4。
再接下来的 V 行中,每行有 2 个城市名,中间用空格隔开,如 city1 city2 表示 city1到 city2 有一条直通航线,从 city2 到 city1 也有一条直通航线。
输出格式:件第 1 行是旅行路线中所访问的城市总数 M。接下来的 M+1 行是旅行路线的城市名,每行写 1 个城市名。首先是出发城市名,然后按访问顺序列出其它城市名。注意,最后 1 行(终点城市)的城市名必然是出发城市名。如果问题无解,则输出“No Solution!”。
输入输出样例
8 9 Vancouver Yellowknife Edmonton Calgary Winnipeg Toronto Montreal Halifax Vancouver Edmonton Vancouver Calgary Calgary Winnipeg Winnipeg Toronto Toronto Halifax Montreal Halifax Edmonton Montreal Edmonton Yellowknife Edmonton Calgary
7 Vancouver Edmonton Montreal Halifax Toronto Winnipeg Calgary Vancouver
说明
感谢 @zhouyonglong 提供spj