二分答案 下等:a-x 中等:b-x 上等:c
1 // #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 11000; 22 const int MOD = 1e9+7; 23 #define LL long long 24 #define mi() (l+r)>>1 25 double const pi = acos(-1); 26 27 // inline int r() { 28 // int x=0,f=1;char ch=getchar(); 29 // while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();} 30 // while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f; 31 // } 32 33 int a,b,c; 34 35 bool b_s(int x){ 36 int w=x; 37 int num[3]; 38 num[0]=a-x,num[1]=b-x,num[2]=c; 39 for(int i=0;i<3;i++){ 40 w-=min(num[i],(x+1)/2); 41 } 42 return w<=0; 43 } 44 45 int main(){ 46 int T; 47 cin>>T; 48 while(T--){ 49 // int a,b,c; 50 cin>>a>>b>>c; 51 int l=0,r=b; 52 int ans; 53 while(l<=r){ 54 int mid=(l+r)>>1; 55 if(b_s(mid)) ans=mid,l=mid+1; 56 else{ 57 r=mid-1; 58 } 59 } 60 cout<<ans<<endl; 61 } 62 return 0; 63 }
之前没有做个网络流建模的题
首先,要经过没有重复的点,可以拆点来做。我们规定一个新源点流向mid,容量为2;a,b分别流向最终的汇点流量为1。这样整体思路就有了。先建立网络流模型,相邻点,相邻边容量为1。除了mid点之间是容量为2。跑一遍ek得到每条边流量。
最后判断满流:流量==容量
1 // #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #include <list> 9 #include <map> 10 #include <vector> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <cstdlib> 15 // #include <conio.h> 16 using namespace std; 17 #define clc(a,b) memset(a,b,sizeof(a)) 18 #define inf 0x3f3f3f3f 19 #define lson l,mid,rt<<1 20 #define rson mid+1,r,rt<<1|1 21 const int N = 110<<2; 22 const int MOD = 1e9+7; 23 #define LL long long 24 #define mi() (l+r)>>1 25 double const pi = acos(-1); 26 27 void fre(){ 28 freopen("in.txt","r",stdin); 29 } 30 31 // inline int r() { 32 // int x=0,f=1;char ch=getchar(); 33 // while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();} 34 // while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f; 35 // } 36 int n,m; 37 int cap[N][N],flow[N][N]; 38 int pre[N]; 39 bool vis[N]; 40 int head[N]; 41 int tot; 42 struct Edge{ 43 int v,next; 44 }e[N]; 45 46 void add(int u,int v){ 47 e[tot]=(Edge){v,head[u]}; 48 head[u]=tot++; 49 } 50 void init(){ 51 clc(flow,0); 52 clc(cap,0); 53 clc(head,-1); 54 tot=0; 55 } 56 void EK(int s,int t){ 57 queue<int>q; 58 clc(flow,0); 59 int u; 60 while(1){ 61 clc(vis,0); 62 int d=inf; 63 while(!q.empty()) q.pop(); 64 vis[s]=1; 65 q.push(s); 66 clc(pre,-1); 67 while(!q.empty()){ 68 u=q.front(); 69 q.pop(); 70 for(int i=0;i<=2*n+1;i++){ 71 if(!vis[i]&&flow[u][i]<cap[u][i]){ 72 d=min(d,cap[u][i]-flow[u][i]); 73 vis[i]=true; 74 pre[i]=u; 75 q.push(i); 76 } 77 } 78 } 79 if(pre[t]==-1) 80 break; 81 for(int i=t;i!=s;i=pre[i]){ 82 flow[pre[i]][i]+=d; 83 flow[i][pre[i]]-=d; 84 } 85 } 86 } 87 vector<int> ans; 88 void dfs(int u,int fa){ 89 ans.push_back(u); 90 for(int i=head[u];~i;i=e[i].next){ 91 int v=e[i].v; 92 // printf("v: %d ",v); 93 if(v==fa) continue; 94 // printf("v: %d ",v); 95 dfs(v,u); 96 } 97 } 98 int main(){ 99 // fre(); 100 int T; 101 int a,b,mid; 102 scanf("%d",&T); 103 while(T--){ 104 init(); 105 cin>>n>>m; 106 cin>>a>>b>>mid; 107 for(int i=1;i<=m;i++){ 108 int u,v; 109 cin>>u>>v; 110 cap[u][v+n]=1; 111 cap[v][u+n]=1; 112 } 113 for(int i=1;i<=n;i++){ 114 if(i!=mid) cap[i+n][i]=1; 115 else cap[mid+n][mid]=2; 116 } 117 cap[0][mid]=2; 118 cap[a][2*n+1]=cap[b][2*n+1]=1; 119 EK(0,2*n+1); 120 for(int i=1;i<=n;i++){ 121 for(int j=n+1;j<=2*n;j++){ 122 if(flow[i][j]&&flow[i][j]==cap[i][j]){ 123 add(i,j-n);add(j-n,i); 124 } 125 } 126 } 127 ans.clear(); 128 dfs(a,0); 129 for(int i=0;i<ans.size();i++){ 130 printf("%d",ans[i]); 131 if(i!=(int)ans.size()-1) 132 printf(" "); 133 } 134 cout<<endl; 135 } 136 return 0; 137 }