zoukankan      html  css  js  c++  java
  • HDOJ 4862 Jump


    K路径覆盖问题,最小费用最大流。。。。


    最小K路径覆盖的模型,用费用流或者KM算法解决,构造二部图,X部有N*M个节点,源点向X部每一个节点连一条边,流量1,费用0Y部有N*M个节点,每一个节点向汇点连一条边,流量1,费用0,假设X部的节点x能够在一步之内到达Y部的节点y,那么就连边x->y,费用为从x格子到y格子的花费能量减去得到的能量,流量1,再在X部添加一个新的节点,表示能够从随意节点出发K次,源点向其连边,费用0,流量K,这个点向Y部每一个点连边,费用0,流量1,最这个图跑最小费用最大流,假设满流就是存在解,反之不存在,最小费用的相反数就是能够获得的最大能量

    Jump

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 546    Accepted Submission(s): 238


    Problem Description
    There are n*m grids, each grid contains a number, ranging from 0-9. Your initial energy is zero. You can play up to K times the game, every time you can choose any one of the grid as a starting point (but not traveled before) then you can choose a grid on the right or below the current grid to jump, but it has not traveled before. Every time you can jump as many times as you want, as long as you do not violate rules. If you are from (x1, y1) to (x2, y2), then you consume |x1-x2|+|y1-y2|-1 energies. Energy can be negative. 
    However, in a jump, if you start position and end position has same numbers S, then you can increase the energy value by S. 
    Give me the maximum energy you can get. Notice that you have to go each grid exactly once and you don’t have to play exactly K times.
     

    Input
    The first line is an integer T, stands for the number of the text cases.
    Then T cases followed and each case begin with three numbers N, M and K. Means there are N rows and M columns, you have K times to play.
    Then N lines follow, each line is a string which is made up by M numbers.
    The grids only contain numbers from 0 to 9.
    (T<=100, N<=10,M<=10,K<=100)
     

    Output
    Each case, The first you should output “Case x : ”,(x starting at 1),then output The maximum number of energy value you can get. If you can’t reach every grid in no more than K times, just output -1.
     

    Sample Input
    5 1 5 1 91929 1 5 2 91929 1 5 3 91929 3 3 3 333 333 333 3 3 2 333 333 333
     

    Sample Output
    Case 1 : 0 Case 2 : 15 Case 3 : 16 Case 4 : 18 Case 5 : -1
     

    Author
    FZU
     

    Source
     

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    
    using namespace std;
    
    const int maxn=110000;
    const int INF=0x3f3f3f3f;
    
    int n,m,k;
    int a[20][20];
    
    struct Edge
    {
    	int to,next,cap,flow,cost;
    }edge[maxn];
    
    int Adj[maxn],Size,N;
    
    void init()
    {
    	memset(Adj,-1,sizeof(Adj)); Size=0;
    }
    
    void addedge(int u,int v,int cap,int cost)
    {
    	edge[Size].to=v;
    	edge[Size].next=Adj[u];
    	edge[Size].cost=cost;
    	edge[Size].cap=cap;
    	edge[Size].flow=0;
    	Adj[u]=Size++;
    }
    
    void Add_Edge(int u,int v,int cap,int cost)
    {
    	addedge(u,v,cap,cost);
    	addedge(v,u,0,-cost);
    }
    
    int dist[1000],vis[1000],pre[1000];
    
    bool spfa(int s,int t)
    {
    	queue<int> q;
    	for(int i=0;i<N;i++)
    	{
    		dist[i]=INF;vis[i]=false; pre[i]=-1;
    	}
    	dist[s]=0; vis[s]=true; q.push(s);
    	while(!q.empty())
    	{
    		int u=q.front();
    		q.pop();
    		vis[u]=false;
    		for(int i=Adj[u];~i;i=edge[i].next)
    		{
    			int v=edge[i].to;
    			if(edge[i].cap>edge[i].flow&&
    				dist[v]>dist[u]+edge[i].cost)
    			{
    				dist[v]=dist[u]+edge[i].cost;
    				pre[v]=i;
    				if(!vis[v])
    				{
    					vis[v]=true;
    					q.push(v);
    				}
    			}
    		}
    	}
    	if(pre[t]==-1) return false;
    	return true;
    }
    
    int MinCostMaxFlow(int s,int t,int& cost)
    {
    	int flow=0;
    	cost=0;
    	while(spfa(s,t))
    	{
    		int Min=INF;
    		for(int i=pre[t];~i;i=pre[edge[i^1].to])
    		{
    			if(Min>edge[i].cap-edge[i].flow)
    				Min=edge[i].cap-edge[i].flow;
    		}
    		for(int i=pre[t];~i;i=pre[edge[i^1].to])
    		{
    			edge[i].flow+=Min;
    			edge[i^1].flow-=Min;
    			cost+=edge[i].cost*Min;
    		}
    		flow+=Min;
    	}
    	return flow;
    }
    char in[10010];
    int main()
    {
    	int T_T,cas=1;
    	scanf("%d",&T_T);
    while(T_T--)
    {
    	init();
    	scanf("%d%d%d",&n,&m,&k);
    	memset(a,0,sizeof(a));
    	for(int i=0;i<n;i++)	
    	{
    		scanf("%s",in);
    		for(int j=0;j<m;j++)
    		{
     			a[i][j]=in[j]-'0';
    		}
    	}
    	///source:2*n*m sink:2*n*m+1 mid:2*n*m+2;
    	int source=2*n*m,sink=2*n*m+1,mid=2*n*m+2;	
    	N=mid+1;
    	Add_Edge(source,mid,k,0);
    	for(int i=0;i<n;i++)
    	{
    		for(int j=0;j<m;j++)
    		{
    			int from=i*m+j;
    			Add_Edge(source,from,1,0);
    			Add_Edge(from+n*m,sink,1,0);
    			Add_Edge(mid,from+n*m,1,0);
    			for(int ii=i+1;ii<n;ii++)
    			{
    				int to=ii*m+j+n*m;
    				int cost=0;
    				if(a[i][j]==a[ii][j])
    					cost=a[i][j];
    				cost-=ii-i-1;
    				Add_Edge(from,to,1,-cost);
    			}
    			for(int jj=j+1;jj<m;jj++)
    			{
    				int to=i*m+jj+n*m;
    				int cost=0;
    				if(a[i][j]==a[i][jj])
    					cost=a[i][j];
    				cost-=jj-j-1;
    				Add_Edge(from,to,1,-cost);
    			}
    		}
    	}
    	int C,F;
    	F=MinCostMaxFlow(source,sink,C);
    	C=-C;
    	if(F!=n*m) C=-1;
    	printf("Case %d : %d
    ",cas++,C);
    }
    	return 0;
    }





  • 相关阅读:
    【Android】: 部分注意事项
    【Java】:多线程下载
    【操作系统】:Main features of the X86-64
    「数据结构」:模拟指针(simulated pointer)
    「JAVA」:Berkeley DB的JAVA连接
    「python」: arp脚本的两种方法
    「数据结构」: 间接寻址
    「操作系统」: Conditional Move Instructions(trap)
    「操作系统」:The most useful condition codes
    quartz定时任务时间设置
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3930820.html
Copyright © 2011-2022 走看看