POJ1815 Friendship 枚举+拆点+最小割

本文介绍了一个基于最小割算法解决特定图论问题的方法。通过构造特殊图结构并利用最小割算法来找出使两个指定节点断开连接所需的最少节点删除数量及具体方案。特别地,文章详细解释了如何通过增加辅助节点和边来实现这一目标。
本题是一个无向图,然后让我们求出去掉最少的点,可以使s和t不连通,并要求我们输出字典序最小的一种方案。
看出了是最小割,但没想到要拆点,又学到了。
建图:
1:把除了s和t外其他所有点连一条弧(i,i+n,1)(这条弧表示i没有丢失自己的联系人,没有这条弧,说明i不能喝他的朋友联系了。因为i能联系到的人我是用i+n相连的)
2:如果i知道s的号码,则连一条弧(s,i,INF),
     如果i知道t的号码,则连一条弧(i+n,t,INF)
3:如果i知道j的号码 ,则连弧(i+n,j,INF) 和(j+n,i,INF)
然后求一个最小割
 
然后是字典序最小得方案输出。
从小到大枚举,确保字典序最小。
先去掉最小的点,判断最小割是否减小,减小则入队,否则从新将点加入。直到最小割变成0为止(此时s到t不连通)
 
 
 
Friendship
Time Limit: 2000MS Memory Limit: 20000K
Total Submissions: 6898 Accepted: 1908

Description

In modern society, each person has his own friends. Since all the people are very busy, they communicate with each other only by phone. You can assume that people A can keep in touch with people B, only if
1. A knows B's phone number, or
2. A knows people C's phone number and C can keep in touch with B.
It's assured that if people A knows people B's number, B will also know A's number.

Sometimes, someone may meet something bad which makes him lose touch with all the others. For example, he may lose his phone number book and change his phone number at the same time.

In this problem, you will know the relations between every two among N people. To make it easy, we number these N people by 1,2,...,N. Given two special people with the number S and T, when some people meet bad things, S may lose touch with T. Your job is to compute the minimal number of people that can make this situation happen. It is supposed that bad thing will never happen on S or T.

Input

The first line of the input contains three integers N (2<=N<=200), S and T ( 1 <= S, T <= N , and S is not equal to T).Each of the following N lines contains N integers. If i knows j's number, then the j-th number in the (i+1)-th line will be 1, otherwise the number will be 0.

You can assume that the number of 1s will not exceed 5000 in the input.

Output

If there is no way to make A lose touch with B, print "NO ANSWER!" in a single line. Otherwise, the first line contains a single number t, which is the minimal number you have got, and if t is not zero, the second line is needed, which contains t integers in ascending order that indicate the number of people who meet bad things. The integers are separated by a single space.

If there is more than one solution, we give every solution a score, and output the solution with the minimal score. We can compute the score of a solution in the following way: assume a solution is A1, A2, ..., At (1 <= A1 < A2 <...< At <=N ), the score will be (A1-1)*N^t+(A2-1)*N^(t-1)+...+(At-1)*N. The input will assure that there won't be two solutions with the minimal score.

Sample Input

3 1 3
1 1 0
1 1 1
0 1 1

Sample Output

1
2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>

using namespace std;

#define mm(a) memset((a),0,sizeof((a)))
#define INF 0xFFFFFF
#define MAXN 500

struct node
{
	int to,c;
	int next;
}e[9999999];

int n,m,en,dis[MAXN],gap[MAXN];
int st,ed;
int g[MAXN];
int matrix[210][210];
int que[MAXN];
bool deleted[MAXN/2];

void add(int a,int b,int c)
{
	e[en].to=b;
	e[en].c=c;
	e[en].next=g[a];
	g[a]=en++;
	e[en].to=a;
	e[en].c=0;
	e[en].next=g[b];
	g[b]=en++;	
}

int sap(int u,int flow)
{
	if(u==ed) return flow;
	int ans=0,i,t;
	for(i=g[u];i!=-1;i=e[i].next)
	{
		if( e[i].c && dis[u]==dis[e[i].to]+1)
		{
			t=sap(e[i].to,min(flow-ans,e[i].c));
			e[i].c-=t,e[i^1].c+=t,ans+=t;
			if(ans==flow) return ans;
		}
	}
	if(dis[st]>=n*2) return ans;
	if(!--gap[dis[u]]) dis[st]=n*2;
	++gap[++dis[u]];
	return ans;
}

void build()
{
	en=0;
	memset(g,-1,sizeof(g));
	for(int i=1;i<=n;i++) 
		if(i!=st && i!=ed && !deleted[i]) add(i,i+n,1);
	for(int i=1;i<=n;i++)
	{
		if(i!=st)
			if(matrix[st][i]) add(st,i,INF);
		if(i!=ed )
			if(matrix[ed][i]) add(i+n,ed+n,INF);
	}
	for(int i=1;i<=n;i++)
	{
		if(i==st || i==ed)	continue;
		for(int j=1;j<=n;j++)
		{
			if(i==j) continue;
			if(matrix[i][j]) add(i+n,j,INF);
			
		}
	}
}

void solve()
{
	int ans=0,temp;
	memset(deleted,0,sizeof(deleted));
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			scanf("%d",&matrix[i][j]);
	if(matrix[st][ed]==1)
	{
		printf("NO ANSWER!\n");
		return;
	}
	build();
	mm(gap),mm(dis);
	ed=ed+n;
	for(gap[0]=n*2;dis[st]<n*2;)
		ans+=sap(st,INF);
	printf("%d\n",ans);
	if(!ans) return ;
	int t=0,f=0;
	for(int i=1,j=ans;j;i++)
	{
		temp=j;
		j=0;
		deleted[i]=true;
		ed=ed-n;
		build();
		mm(gap),mm(dis);
		ed+=n;
		for(gap[0]=n*2;dis[st]<n*2;)
			j+=sap(st,INF);
		if(j<temp)
			que[t++]=i;
		else
			deleted[i]=false;
	}
	while(f<t)
	{
		printf("%d",que[f++]);
		if(f!=t)
			printf(" ");
	}
	printf("\n");	
}

int main()
{
	while(scanf("%d%d%d",&n,&st,&ed)!=EOF) solve();
	return 0;
}

内容概要:本研究聚焦于绿电直连型电氢氨园区的优化运行,提出一种集成绿色电力直接供给、电解水制氢及氢气合成氨工艺的综合能源系统架构。通过建立包含风光发电、电解槽、氨合成反应器、储氢罐、电网交互及多类型负荷在内的系统模型,综合考虑绿电直供优先、能量梯级利用与多能互补原则,构建以系统综合运行成本最小化为目标的优化调度模型。研究采用Matlab与Python工具进行算法求解和仿真分析,利用实际气象与负荷数据完成案例验证,评估了不同运行策略下系统的经济性、可再生能源消纳能力与碳减排效益,为新型电氢氨一体化园区的规划与运行提供了理论依据和技术支撑。; 适合人群:具备一定电力系统、新能源或化工背景的研究生、科研人员及从事综合能源系统规划与优化工作的工程技术人员。; 使用场景及目标:①用于科研学习,理解电-氢-氨多能转换系统的建模与优化方法;②为工业园区的低碳化、智能化改造提供技术参考与决策支持;③作为开发类似综合能源管理系统的理论基础。; 阅读建议:此资源包含完整的模型代码、数据与论文,使用者应结合代码仔细研读论文中的模型构建部分,重关注目标函数与约束条件的设计逻辑,并尝试修改参数进行仿真,以深入掌握优化算法在实际系统中的应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值