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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

CodeForces - 1426E Rock, Paper, Scissors(最小费用最大流+最大费用最大流)

發布時間:2024/4/11 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 1426E Rock, Paper, Scissors(最小费用最大流+最大费用最大流) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:A 和 B 在玩石頭剪刀布,A 會出 a1 次石頭,a2 次剪刀,a3 次布,同理 B 會出 b1 次石頭,b2 次剪刀,b3 次布,若對戰順序是可以進行決定的,問 A 最少能贏多少次,最多能贏多少次

題目分析:一道需要分類討論非常復雜的貪心題,這里提供一種費用流無腦暴力的方法:

  • st -> A 的三種決策,流量分別為 a1 , a2 , a3 ,花費為 0
  • A 的石頭 ->
  • B 的剪刀:流量為 inf ,花費為 1
  • B 的石頭:流量為 inf ,花費為 0
  • B 的布:流量為 inf , 花費為 0
  • A 的剪刀 ->
  • B 的剪刀:流量為 inf ,花費為 0
  • B 的石頭:流量為 inf ,花費為 0
  • B 的布:流量為 inf , 花費為 1
  • A 的布 ->
  • B 的剪刀:流量為 inf ,花費為 0
  • B 的石頭:流量為 inf ,花費為 1
  • B 的布:流量為 inf , 花費為 0
  • B 的三種決策 -> ed,流量分別為 b1 , b2 , b3 ,花費為 0
  • 答案對應的就是最小費用最大流和最大費用最大流了

    代碼:
    ?

    //#pragma GCC optimize(2) //#pragma GCC optimize("Ofast","inline","-ffast-math") //#pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<unordered_map> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e5+100;//��const int M=1e5+100;//��int a1,a2,a3,b1,b2,b3;int st=N-1,ed=st-1,n;struct Edge {int to,w,cost,next; }edge[M];int head[N],cnt;void addedge(int u,int v,int w,int cost) {edge[cnt].to=v;edge[cnt].w=w;edge[cnt].cost=cost;edge[cnt].next=head[u];head[u]=cnt++;edge[cnt].to=u;edge[cnt].w=0;edge[cnt].cost=-cost;edge[cnt].next=head[v];head[v]=cnt++; }int d[N],incf[N],pre[N];bool vis[N];bool spfa1(int s,int t) {memset(d,inf,sizeof(d));memset(vis,false,sizeof(vis));memset(pre,-1,sizeof(pre));queue<int>q;q.push(s);vis[s]=true;incf[s]=inf;d[s]=0;while(!q.empty()){int u=q.front();q.pop();vis[u]=false;for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;int w=edge[i].w;int cost=edge[i].cost;if(!w)continue;if(d[v]>d[u]+cost){d[v]=d[u]+cost;pre[v]=i;incf[v]=min(incf[u],w);if(!vis[v]){vis[v]=true;q.push(v);}}}}return pre[t]!=-1; }int update1(int s,int t) {int x=t;while(x!=s){int i=pre[x];edge[i].w-=incf[t];edge[i^1].w+=incf[t];x=edge[i^1].to;}return d[t]*incf[t]; }void init() {memset(head,-1,sizeof(head));cnt=0; }int solve1(int st,int ed) {int ans=0;while(spfa1(st,ed))ans+=update1(st,ed);return ans; }bool spfa(int s,int t) {memset(d,0xcf,sizeof(d));memset(vis,false,sizeof(vis));memset(pre,-1,sizeof(pre));queue<int>q;q.push(s);vis[s]=true;incf[s]=inf;d[s]=0;while(!q.empty()){int u=q.front();q.pop();vis[u]=false;for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;int w=edge[i].w;int cost=edge[i].cost;if(!w)continue;if(d[v]<d[u]+cost){d[v]=d[u]+cost;pre[v]=i;incf[v]=min(incf[u],w);if(!vis[v]){vis[v]=true;q.push(v);}}}}return pre[t]!=-1; }int update(int s,int t) {int x=t;while(x!=s){int i=pre[x];edge[i].w-=incf[t];edge[i^1].w+=incf[t];x=edge[i^1].to;}return d[t]*incf[t]; }int solve(int st,int ed) {int ans=0;while(spfa(st,ed))ans+=update(st,ed);return ans; }void build() {init();addedge(st,1,a1,0);addedge(st,2,a2,0);addedge(st,3,a3,0);addedge(4,ed,b1,0);addedge(5,ed,b2,0);addedge(6,ed,b3,0);addedge(1,4,inf,0);addedge(1,5,inf,1);addedge(1,6,inf,0);addedge(2,4,inf,0);addedge(2,5,inf,0);addedge(2,6,inf,1);addedge(3,4,inf,1);addedge(3,5,inf,0);addedge(3,6,inf,0); }int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);cin>>n>>a1>>a2>>a3>>b1>>b2>>b3;build();printf("%d",solve1(st,ed));build();printf(" %d",solve(st,ed));return 0; }

    ?

    總結

    以上是生活随笔為你收集整理的CodeForces - 1426E Rock, Paper, Scissors(最小费用最大流+最大费用最大流)的全部內容,希望文章能夠幫你解決所遇到的問題。

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