標籤:why pop 模型 java tar pos integer turn ted
Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2650 Accepted Submission(s): 1411
Problem DescriptionYifenfei very like play a number game in the n*n Matrix. A positive integer number is put in each area of the Matrix.
Every time yifenfei should to do is that choose a detour which frome the top left point to the bottom right point and than back to the top left point with the maximal values of sum integers that area of Matrix yifenfei choose. But from the top to the bottom can only choose right and down, from the bottom to the top can only choose left and up. And yifenfei can not pass the same area of the Matrix except the start and end.
InputThe input contains multiple test cases.
Each case first line given the integer n (2<n<30)
Than n lines,each line include n positive integers.(<100)
OutputFor each test case output the maximal values yifenfei can get.
Sample Input210 35 10310 3 32 5 36 7 1051 2 3 4 52 3 4 5 63 4 5 6 74 5 6 7 85 6 7 8 9
Sample Output284680
Authoryifenfei
SourceZJFC 2009-3 Programming Contest
Recommendyifenfei
//費用流模板題,因為題目中的表述就是一個網路流模型,拆點建圖,沒點只經過一次,起點終點容量為2,//其他點容量為1,要求最大費用費用為負權值,套模板。起點和終點經曆了兩次,最後要減去。/****************最小費用最大流模板,白書363頁*******************************/#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<vector>using namespace std;const int maxn=30*30*2+5,inf=0x7fffffff;//本體拆點,數組多開兩倍struct Edge{ int from,to,cap,flow,cost; Edge(int u,int v,int c,int f,int cs):from(u),to(v),cap(c),flow(f),cost(cs){}};struct MCMF{ int n,m,s,t; vector<Edge>edges; vector<int>g[maxn]; int inq[maxn],d[maxn],p[maxn],a[maxn]; void init(int n) { this->n=n; for(int i=0;i<n;i++) g[i].clear(); edges.clear(); } void AddEdge(int from,int to,int cap,int cost) { edges.push_back((Edge){from,to,cap,0,cost}); edges.push_back((Edge){to,from,0,0,-cost}); m=edges.size(); g[from].push_back(m-2); g[to].push_back(m-1); } bool BellmanFord(int s,int t,int &flow,int &cost) { for(int i=0;i<n;i++) d[i]=inf; memset(inq,0,sizeof(inq)); d[s]=0;inq[s]=1;p[s]=0;a[s]=inf; queue<int>q; q.push(s); while(!q.empty()){ int u=q.front();q.pop(); inq[u]=0; for(int i=0;i<(int)g[u].size();i++){ Edge &e=edges[g[u][i]]; if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){ d[e.to]=d[u]+e.cost; p[e.to]=g[u][i]; a[e.to]=min(a[u],e.cap-e.flow); if(!inq[e.to]) {q.push(e.to);inq[e.to]=1;} } } } if(d[t]==inf) return false; flow+=a[t]; cost+=d[t]*a[t]; int u=t; while(u!=s){ edges[p[u]].flow+=a[t]; edges[p[u]^1].flow-=a[t]; u=edges[p[u]].from; } return true; } int Mincost(int s,int t) { int flow=0,cost=0; while(BellmanFord(s,t,flow,cost)); return cost;//返回最小費用,flow存最大流 }}MC;/**********************************************************************************/int main(){ int n,mp[35][35]; while(scanf("%d",&n)==1){ int s=0,t=n*n*2+1; MC.init(n*n*2+2); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&mp[i][j]); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ int id=(i-1)*n+j; if(id==1){ MC.AddEdge(id,id+n*n,2,-mp[i][j]); MC.AddEdge(s,id,2,0); } else if(id==n*n){ MC.AddEdge(id,id+n*n,2,-mp[i][j]); MC.AddEdge(id+n*n,t,2,0); } else MC.AddEdge(id,id+n*n,1,-mp[i][j]); if(i<n){ int nid=id+n; MC.AddEdge(id+n*n,nid,1,0); } if(j<n){ int nid=id+1; MC.AddEdge(id+n*n,nid,1,0); } } } int ans=-(MC.Mincost(0,n*n*2+1)+mp[1][1]+mp[n][n]); printf("%d\n",ans); } return 0;}
Matrix Again
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 4255 Accepted Submission(s): 1233
Problem DescriptionStarvae very like play a number game in the n*n Matrix. A positive integer number is put in each area of the Matrix.
Every time starvae should to do is that choose a detour which from the top left point to the bottom right point and than back to the top left point with the maximal values of sum integers that area of Matrix starvae choose. But from the top to the bottom can only choose right and down, from the bottom to the top can only choose left and up. And starvae can not pass the same area of the Matrix except the start and end..
Do you know why call this problem as “Matrix Again”? AS it is like the problem 2686 of HDU.
InputThe input contains multiple test cases.
Each case first line given the integer n (2<=n<=600)
Then n lines, each line include n positive integers. (<100)
OutputFor each test case output the maximal values starvae can get.
Sample Input2 10 3 5 10 3 10 3 3 2 5 3 6 7 10 5 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
Sample Output28 46 80
AuthorStarvae
SourceHDOJ Monthly Contest – 2010.04.04
//和HDU2686一樣,只是資料變大了,上一個模板會超記憶體,這個板不會。/***********************最小費用最大流模板2*************************/#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;const int maxn=610*610*2+2;const int maxm=4*maxn;//!邊數要夠const int inf=0x7fffffff;struct Edge{ int to,next,cap,flow,cost;}edges[maxm];int head[maxn],tol,pre[maxn],dis[maxn];bool vis[maxn];int N;void init(int n){ N=n; tol=0; memset(head,-1,sizeof(head));}void AddEdge(int u,int v,int cap,int cost){ edges[tol].to=v; edges[tol].cap=cap; edges[tol].cost=cost; edges[tol].flow=0; edges[tol].next=head[u]; head[u]=tol++; edges[tol].to=u; edges[tol].cap=0; edges[tol].cost=-cost; edges[tol].flow=0; edges[tol].next=head[v]; head[v]=tol++;}bool spfa(int s,int t){ queue<int>q; for(int i=0;i<=N;i++){ dis[i]=inf; vis[i]=0; pre[i]=-1; } dis[s]=0; vis[s]=1; q.push(s); while(!q.empty()){ int u=q.front();q.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=edges[i].next){ int v=edges[i].to; if(edges[i].cap>edges[i].flow&&dis[v]>dis[u]+edges[i].cost){ dis[v]=dis[u]+edges[i].cost; pre[v]=i; if(!vis[v]) {vis[v]=1;q.push(v);} } } } if(pre[t]==-1) return 0; return 1;}int MinCostFlow(int s,int t){ int flow=0,cost=0; while(spfa(s,t)){ int Min=inf; for(int i=pre[t];i!=-1;i=pre[edges[i^1].to]) Min=min(Min,edges[i].cap-edges[i].flow); for(int i=pre[t];i!=-1;i=pre[edges[i^1].to]){ edges[i].flow+=Min; edges[i^1].flow-=Min; cost+=edges[i].cost*Min; } flow+=Min; } return cost;//返回最小費用,flow存最大流}/*********************************************************************/int main(){ int n,mp[605][605]; while(scanf("%d",&n)==1){ for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&mp[i][j]); int s=0,t=n*n*2+1; init(n*n*2+2); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ int id=(i-1)*n+j; if(id==1){ AddEdge(id,id+n*n,2,-mp[i][j]); AddEdge(s,id,2,0); } else if(id==n*n){ AddEdge(id,id+n*n,2,-mp[i][j]); AddEdge(id+n*n,t,2,0); } else AddEdge(id,id+n*n,1,-mp[i][j]); if(i<n) AddEdge(id+n*n,id+n,1,0); if(j<n) AddEdge(id+n*n,id+1,1,0); } } int ans=-(MinCostFlow(s,t)+mp[1][1]+mp[n][n]); printf("%d\n",ans); } return 0;}
給出代碼供以後參考學習
多線程DP