HDU 4265(Science!-二分网络流)

Science!

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 415    Accepted Submission(s): 116
Special Judge


Problem Description
Welcome, ladies and gentlemen, to Aperture Science. Astronauts, War Heroes, Olympians — you’re here because we want the best, and you are it. That said, it’s time to make some science.
Now, I want each of you to stand on one of these buttons. Well done, we’re making great progress here. Now let’s do it again. Oh, come on - don’t stand on the same button! Move, people! No, no, that button’s only for the Astronauts, you know who you are. What?! You say you can’t do everything I ask? Ok let’s start over. You there, the Programmer, figure out how many times we can do this. And make it quick, we have a lot more science to get through…
 

Input
There will be several test cases in the input. The first line of each case will contain n (2≤ n≤80) giving the number of people (and the number of buttons) in the experiment. The next n lines will contain n characters each. If the j th character of the i th line is Y it indicates that the i th person can stand on the j th button (it is N otherwise). The last line of input will be a 0.
 

Output
For each test case, output k, the maximum number of times everyone can be standing on buttons such that nobody stands on the same button more than once (This might be 0). After that, output k lines. Each line should contain n integers separated by single spaces, where the i th integer describes which person is standing on the i th button. All of the lines should be valid and none of them should put the same person on the same button as a previous line of the same test case. Output no extra spaces, and do not separate answers with blank lines. Note that correct outputs might not be unique.
 

Sample Input
  
3 YYY NYY YNY 2 YN YN 0
 

Sample Output
  
2 3 1 2 1 2 3 0
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   5390  5389  5388  5387  5386 
 

二分答案网络流,求出k.

删除多余边,

接下来k次,

每次在原图上找最大二分图匹配,删除边,

得到一个可行解




#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXn (80+10)
#define MAXN (500+19)
#define MAXM (69999+100)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
class Max_flow  //dinic+当前弧优化   
{    
public:    
    int n,s,t;    
    int q[MAXN];    
    int edge[MAXM],next[MAXM],pre[MAXN],weight[MAXM],size;    
    void addedge(int u,int v,int w)      
    {      
        edge[++size]=v;      
        weight[size]=w;      
        next[size]=pre[u];      
        pre[u]=size;      
    }      
    void addedge2(int u,int v,int w){addedge(u,v,w),addedge(v,u,0);}     
    bool b[MAXN];    
    int d[MAXN];    
    bool SPFA(int s,int t)      
    {      
        For(i,n) d[i]=INF;    
        MEM(b)    
        d[q[1]=s]=0;b[s]=1;      
        int head=1,tail=1;      
        while (head<=tail)      
        {      
            int now=q[head++];      
            Forp(now)      
            {      
                int &v=edge[p];      
                if (weight[p]&&!b[v])      
                {      
                    d[v]=d[now]+1;      
                    b[v]=1,q[++tail]=v;      
                }      
            }          
        }      
        return b[t];      
    }     
    int iter[MAXN];  
    int dfs(int x,int f)  
    {  
        if (x==t) return f;  
        Forpiter(x)  
        {  
            int v=edge[p];  
            if (weight[p]&&d[x]<d[v])  
            {  
                  int nowflow=dfs(v,min(weight[p],f));  
                  if (nowflow)  
                  {  
                    weight[p]-=nowflow;  
                    weight[p^1]+=nowflow;  
                    return nowflow;  
                  }  
            }  
        }  
        return 0;  
    }  
    int max_flow(int s,int t)  
    {  
        int flow=0;  
        while(SPFA(s,t))  
        {  
            For(i,n) iter[i]=pre[i];  
            int f;  
            while (f=dfs(s,INF))  
                flow+=f;   
        }  
        return flow;  
    }   
    void mem(int n,int s,int t)    
    {    
        (*this).n=n;  
        (*this).t=t;    
        (*this).s=s;    
          
        size=1;    
        MEM(pre)   
    }    
}S;    

char s[MAXn][MAXn];
int n;
bool check(int m)
{
	S.mem(2*n+2,1,2*n+2);
	For(i,n) S.addedge2(1,i+1,m),S.addedge2(n+1+i,2*n+2,m);
	For(i,n) For(j,n) if (s[i][j]=='Y') S.addedge2(1+i,n+1+j,1);

	if (S.max_flow(1,2*n+2)==n*m) return 1;
	return 0;
}

int Ans[MAXn];

int main()
{
//	freopen("hdu4265.in","r",stdin);
//	freopen(".out","w",stdout);
	
	while(scanf("%d",&n)==1)
	{
		if (!n) break;
		For(i,n) scanf("%s",s[i]+1);
		
		int l=0,r=n,ans=0;
		while(l<=r)
		{
			int m=(l+r)>>1;
			if (check(m)) ans=m,l=m+1;
			else r=m-1;
		}
		
		check(ans);
			
		printf("%d\n",ans);
		Fork(i,2,n+1)
		{
			for(int p=S.pre[i];p;p=S.next[p])
				if (!S.weight[p^1]&&S.edge[p]>n+1) s[i-1][S.edge[p]-n-1]='N'; 
			
		}
	
		while (ans--)
		{
			S.mem(2*n+2,1,2*n+2);
			For(i,n) S.addedge2(1,i+1,1),S.addedge2(n+1+i,2*n+2,1);
			For(i,n) For(j,n) if (s[i][j]=='Y') S.addedge2(1+i,n+1+j,1);
			S.max_flow(1,2*n+2);
			
		
			
			Fork(i,2,n+1)
			{
				for(int p=S.pre[i];p;p=S.next[p])
					if (!S.weight[p]&&S.edge[p]>n+1)
					{
						Ans[S.edge[p]-n-1]=i-1,s[i-1][S.edge[p]-n-1]='N'; 
						break;
					}
			}
			
			For(i,n-1) printf("%d ",Ans[i]);printf("%d\n",Ans[n]);
	
			
		}	
		
	}
	
	return 0;
}









内容概要:本文围绕“考虑电能交互的冷热电区域多微网系统双层多场景协同优化配置”的Matlab代码实现展开,提出一种结合电能交互机制的双层优化模型,用于解决冷、热、电多能耦合背景下多微网系统的协同规划与运行问题。研究采用多场景分析方法应对可再生能源出力与负荷需求的不确定性,通过上层规划设备容量配置与下层优化多时段运行策略的联动,提升系统在复杂环境下的经济性、鲁棒性与能源利用效率。所提供的Matlab代码集成了建模、求解(如YALMIP+CPLEX)与结果可视化全流程,涵盖场景生成与削减、双层优化结构设计及多能流协同调度等关键技术环节,为综合能源系统优化提供了完整的算法实现与技术参考。; 适合人群:具备电力系统、综合能源系统或优化建模背景,熟悉Matlab编程与数学规划方法,正在从事相关领域科研或工程设计工作的研究生、高校研究人员及能源行业技术人员。; 使用场景及目标:①开展冷热电联供(CCHP)多微网系统的容量规划与运行优化研究;②支撑含分布式能源、储能及多能转换设备的综合能源系统多目标、多场景优化建模;③学习与复现双层优化、分布鲁棒优化及场景分析等先进优化方法在能源系统中的实际应用。; 阅读建议:建议结合配套文献与代码同步研读,重点理解双层模型的构建逻辑、变量耦合关系与求解技巧,关注场景生成方法与YALMIP调用细节,通过调整参数、修改目标函数等方式进行仿真实验,以深化对系统优化机理的掌握。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值