日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[网络流24题]最小路径覆盖问题

發布時間:2025/6/15 编程问答 11 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [网络流24题]最小路径覆盖问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

題目描述

對于一個路徑覆蓋會有兩個性質:

1.每個點屬于且只屬于一條路徑;

2.在每條路徑中,除終點外,每個點只有一條邊可以通向路徑中的另外一個點。

所以可以把每個點拆成兩個點,一個是起始點,一個是目標點,建立二分圖模型。

二分圖中的任何一種匹配都對應著一種路徑覆蓋方案。

若匹配數為零,那么路徑數=總點數,每增加一個匹配,就會減少一條路徑,所以有

        最小路徑覆蓋數=總點數-最大匹配數

再在匹配過程中記錄路徑最后輸出即可。

#include<complex> #include<cstdio> using namespace std; const int INF=0x3f3f3f3f; const int N=401,M=6007; struct node{int v,f,nxt; }e[M<<1]; int n,m,Enum=1,s,t,ans; int front[N],cur[N],deep[N],path[N]; int q[N]; bool vis[N]; int qread() {int x=0;char ch=getchar();while(ch<'0' || ch>'9')ch=getchar();while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}return x; } void Insert(int u,int v) {e[++Enum].v=v;e[Enum].f=1;e[Enum].nxt=front[u];front[u]=Enum;e[++Enum].v=u;e[Enum].nxt=front[v];front[v]=Enum; } bool bfs() {for(int i=0;i<=t;i++){deep[i]=0;cur[i]=front[i];}int head=1,tail=0,u,v;deep[s]=1;q[++tail]=s;while(head<=tail){u=q[head++];for(int i=front[u];i;i=e[i].nxt){v=e[i].v;if(!deep[v] && e[i].f){deep[v]=deep[u]+1;if(v==t)return 1;q[++tail]=v;}}}return 0; } int dfs(int x,int cur_flow) {if(x==t)return cur_flow;int rest=cur_flow,v;for(int &i=cur[x];i;i=e[i].nxt){v=e[i].v;if(deep[v]==deep[x]+1 && e[i].f && rest){int new_flow=dfs(v,min(e[i].f,rest));e[i].f-=new_flow;e[i^1].f+=new_flow;rest-=new_flow;if(new_flow)path[x]=v-n;if(!rest)return cur_flow;}}deep[x]=0;return cur_flow-rest; } void Dinic() {while(bfs())ans+=dfs(s,INF); } void print(int x) {if(!x)return;printf("%d ",x);vis[x]=1;print(path[x]); } int main() {scanf("%d%d",&n,&m);s=0;t=n+n+1;int u,v;for(int i=1;i<=m;i++){u=qread();v=qread();Insert(u,v+n);}for(int i=1;i<=n;i++){Insert(s,i);Insert(i+n,t);}Dinic();for(int i=1;i<=n;i++)if(!vis[i]){print(i);puts("");}printf("%d\n",n-ans);return 0; }

?

轉載于:https://www.cnblogs.com/LeTri/p/8757649.html

總結

以上是生活随笔為你收集整理的[网络流24题]最小路径覆盖问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。