HDU 3315 My Brute (cost Stream)
Address: HDU 3315
The idea of this question is completely self-developed, and I feel quite clever... (Do not spray the ox ...) I have more confidence in building bold images.
The specific idea is to construct a bipartite graph. The Si source and Xi connection points have 1 traffic and the cost is 0. then, when Si can win Xj, the two will be connected to one edge and the cost is-Vi * 1000. If I = j, the cost is reduced by 1, because the question requires that the original sequence be not changed as much as possible, we recommend that you use the same serial number as possible. If the cost value is reduced by 1, the same serial number will be given priority, and the cost will be increased by 1000 times. At this time, the main score factor will not be changed. Similarly, if you lose, the cost value is Vi * 1000. If I = j, the cost value is also reduced by 1.
The final maximum fee cost/1000 is the correct fee value, and cost % 1000 is the number of changed orders. Then make the corresponding judgment and calculation.
The Code is as follows:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include using namespace std;const int INF=0x3f3f3f3f;int head[1000], source, sink, cnt, fei[10000], q[10000], flow, cost;int d[1000], vis[1000], cur[1000], f[1000];struct node{ int u, v, cap, cost, next;}edge[1000000];void add(int u, int v, int cap, int cost){ edge[cnt].v=v; edge[cnt].cap=cap; edge[cnt].cost=cost; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].cap=0; edge[cnt].cost=-cost; edge[cnt].next=head[v]; head[v]=cnt++;}int spfa(){ memset(d,INF,sizeof(d)); memset(vis,0,sizeof(vis)); queue
q; q.push(source); d[source]=0; f[source]=INF; cur[source]=-1; while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(d[v]>d[u]+edge[i].cost&&edge[i].cap) { d[v]=d[u]+edge[i].cost; f[v]=min(f[u],edge[i].cap); cur[v]=i; if(!vis[v]) { vis[v]=1; q.push(v); } } } } if(d[sink]==INF) return 0; flow+=f[sink]; cost-=f[sink]*d[sink]; for(int i=cur[sink];i!=-1;i=cur[edge[i^1].v]) { edge[i].cap-=f[sink]; edge[i^1].cap+=f[sink]; } return 1;}void mcmf(int n){ flow=cost=0; while(spfa()) ; if(cost/1000<=0) { printf("Oh, I lose my dear seaco!\n"); //printf("%d\n",cost); } else { int x=cost/1000, y=cost%1000; printf("%d %.3lf%%\n",x,y*100.0/n); }}int pan(int x1, int x2, int y1, int y2){ while(1) { x2-=y1; if(x2<=0) return 1; x1-=y2; if(x1<=0) return 0; }}int main(){ int n, i, V[100], H[100], P[100], A[100], B[100], j; while(scanf("%d",&n)!=EOF&&n) { for(i=1;i<=n;i++) scanf("%d",&V[i]); for(i=1;i<=n;i++) scanf("%d",&H[i]); for(i=1;i<=n;i++) scanf("%d",&P[i]); for(i=1;i<=n;i++) scanf("%d",&A[i]); for(i=1;i<=n;i++) scanf("%d",&B[i]); memset(head,-1,sizeof(head)); cnt=0; source=0; sink=2*n+1; for(i=1;i<=n;i++) { add(source,i,1,0); add(i+n,sink,1,0); } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(pan(H[i],P[j],A[i],B[j])) { if(i==j) add(i,j+n,1,-V[i]*1000-1); else add(i,j+n,1,-V[i]*1000); } else { if(i==j) add(i,j+n,1,V[i]*1000-1); else add(i,j+n,1,V[i]*1000); } } } mcmf(n); } return 0;}