zoukankan      html  css  js  c++  java
  • 【网络流24题23】火星探险问题

    题面戳我
    不想放题面了。。。

    sol

    首先还是费用流建模,建模方式参见【网络流24题20】深海机器人问题。这题的题解我还没有写。。。
    针对输出方案:在残余网络上搞一搞可以求得每个位置((x,y))被经过的次数。然后做k(探测车的数量)次,每次从((1,1))沿着一条剩余次数大于0的路径跑到((n,m)),把经过的点上面的次数全部减一。这样走肯定是对的。

    code

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    using namespace std;
    const int inf = 1e9;
    const int N = 40;
    struct edge{int to,next,cap,w,cost;}a[N*N<<5];
    int k,n,m,num,s,t,mmp[N][N],P[N][N],head[2*N*N],cnt=1,dis[2*N*N],vis[2*N*N],pe[2*N*N],tot[N][N];
    queue<int>Q;
    int gi()
    {
    	int x=0,w=1;char ch=getchar();
    	while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    	if (ch=='-') w=0,ch=getchar();
    	while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    	return w?x:-x;
    }
    void link(int u,int v,int w,int cost)
    {
    	a[++cnt]=(edge){v,head[u],w,w,cost};
    	head[u]=cnt;
    	a[++cnt]=(edge){u,head[v],0,0,-cost};
    	head[v]=cnt;
    }
    bool spfa()
    {
    	memset(dis,63,sizeof(dis));
    	dis[s]=0;Q.push(s);
    	while (!Q.empty())
    	{
    		int u=Q.front();Q.pop();
    		for (int e=head[u];e;e=a[e].next)
    		{
    			int v=a[e].to;
    			if (a[e].w&&dis[v]>dis[u]+a[e].cost)
    			{
    				dis[v]=dis[u]+a[e].cost;pe[v]=e;
    				if (!vis[v]) vis[v]=1,Q.push(v);
    			}
    		}
    		vis[u]=0;
    	}
    	if (dis[t]==dis[0]) return false;
    	int sum=inf;
    	for (int i=t;i!=s;i=a[pe[i]^1].to)
    		sum=min(sum,a[pe[i]].w);
    	for (int i=t;i!=s;i=a[pe[i]^1].to)
    		a[pe[i]].w-=sum,a[pe[i]^1].w+=sum;
    	return true;
    }
    int main()
    {
    	k=gi();m=gi();n=gi();
    	for (int i=1;i<=n;i++)
    		for (int j=1;j<=m;j++)
    		{
    			mmp[i][j]=gi();
    			P[i][j]=++num;
    		}
    	for (int i=1;i<=n;i++)
    		for (int j=1;j<=m;j++)
    		{
    			if (mmp[i][j]==2)
    				link(P[i][j],P[i][j]+num,1,-1);
    			if (mmp[i][j]!=1)
    				link(P[i][j],P[i][j]+num,inf,0);
    			if (i<n)
    				link(P[i][j]+num,P[i+1][j],inf,0);
    			if (j<m)
    				link(P[i][j]+num,P[i][j+1],inf,0);
    		}
    	s=num*2+1;link(s,P[1][1],k,0);
    	t=s+1;link(P[n][m]+num,t,k,0);
    	while (spfa());
    	for (int i=1;i<=n;i++)
    		for (int j=1;j<=m;j++)
    			for (int e=head[P[i][j]];e;e=a[e].next)
    				if (a[e].to==P[i][j]+num)
    					tot[i][j]+=a[e].cap-a[e].w;
    	for (int t=1;t<=k;t++)
    	{
    		int x=1,y=1;
    		while (x!=n||y!=m)
    		{
    			if (tot[x][y+1])
    				++y,--tot[x][y],printf("%d 1
    ",t);
    			else
    				++x,--tot[x][y],printf("%d 0
    ",t);
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    mac redis 安装及基本设置 python操作redis
    mac webstorm自动编译typescript配置
    MySQL数据库的基本操作
    python 面试基础考试题收集
    pyhon 列表的增删改查
    python 文件读取方法详解
    MAC下绕开百度网盘限速下载的方法,三步操作永久生效
    浏览器窗口输入网址后发生的一段事情(http完整请求)
    CMDB
    django适当进阶篇
  • 原文地址:https://www.cnblogs.com/zhoushuyu/p/8191878.html
Copyright © 2011-2022 走看看