1797: [Ahoi2009]Mincut 最小割

14 篇文章 0 订阅
9 篇文章 0 订阅

1797: [Ahoi2009]Mincut 最小割

Time Limit: 10 Sec   Memory Limit: 162 MB
Submit: 1893   Solved: 824
[ Submit][ Status][ Discuss]

Description

A,B两个国家正在交战,其中A国的物资运输网中有N个中转站,M条单向道路。设其中第i (1≤i≤M)条道路连接了vi,ui两个中转站,那么中转站vi可以通过该道路到达ui中转站,如果切断这条道路,需要代价ci。现在B国想找出一个路径切断方案,使中转站s不能到达中转站t,并且切断路径的代价之和最小。 小可可一眼就看出,这是一个求最小割的问题。但爱思考的小可可并不局限于此。现在他对每条单向道路提出两个问题: 问题一:是否存在一个最小代价路径切断方案,其中该道路被切断? 问题二:是否对任何一个最小代价路径切断方案,都有该道路被切断? 现在请你回答这两个问题。

Input

第一行有4个正整数,依次为N,M,s和t。第2行到第(M+1)行每行3个正 整数v,u,c表示v中转站到u中转站之间有单向道路相连,单向道路的起点是v, 终点是u,切断它的代价是c(1≤c≤100000)。 注意:两个中转站之间可能有多条道路直接相连。 同一行相邻两数之间可能有一个或多个空格。

Output

对每条单向边,按输入顺序,依次输出一行,包含两个非0即1的整数,分 别表示对问题一和问题二的回答(其中输出1表示是,输出0表示否)。 同一行相邻两数之间用一个空格隔开,每行开头和末尾没有多余空格。

Sample Input

6 7 1 6
1 2 3
1 3 2
2 4 4
2 5 1
3 5 5
4 6 2
5 6 3

Sample Output

1 0
1 0
0 0
1 0
0 0
1 0
1 0

HINT

设第(i+1)行输入的边为i号边,那么{1,2},{6,7},{2,4,6}是仅有的三个最小代价切割方案。它们的并是{1,2,4,6,7},交是 。 【数据规模和约定】 测试数据规模如下表所示 数据编号 N M 数据编号 N M 1 10 50 6 1000 20000 2 20 200 7 1000 40000 3 200 2000 8 2000 50000 4 200 2000 9 3000 60000 5 1000 20000 10 4000 60000



2015.4.16新加数据一组,可能会卡掉从前可以过的程序。

Source

[ Submit][ Status][ Discuss]

先跑一个最大流,对残量网络做一些处理
残量网络貌似就是所有不满的边整起来??
先用tarjan在残量网络缩点
对于每条满流的边,如果它连通的是两个分属不同scc的点,那么这条边可能属于一个最小割集
因为如果两个点属于同一个scc,你就算删掉这条边,还是能从s->t
然后一条边如果连通的是s所在的scc和t所在的scc,那这条边就满足第二问
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;

const int maxn = 4E3 + 40;
const int maxm = 6E4 + 60;
const int INF = ~0U>>1;

struct E{
	int to,cap,flow;
	E (int _to = 0,int _cap = 0,int _flow = 0) {to = _to; cap = _cap; flow = _flow;}
}edgs[maxm*2];

int n,m,S,T,cnt,Cnt,scc_num,dfs_clock,cur[maxn],belong[maxn],L[maxn]
	,low[maxn],dfn[maxn],vis[maxn],ans1[maxm],ans2[maxm];

vector <int> v[maxn];
queue <int> Q;
stack <int> s;

void Add(int x,int y,int w)
{
	v[x].push_back(cnt); edgs[cnt++] = E(y,w,0);
	v[y].push_back(cnt); edgs[cnt++] = E(x,0,0);
}

bool BFS()
{
	vis[S] = ++Cnt; L[S] = 1; Q.push(S);
	while (!Q.empty()) {
		int k = Q.front(); Q.pop();
		for (int i = 0; i < v[k].size(); i++) {
			E e = edgs[v[k][i]];
			if (e.cap == e.flow) continue;
			if (vis[e.to] == Cnt) continue;
			vis[e.to] = Cnt;
			L[e.to] = L[k] + 1;
			Q.push(e.to);
		}
	}
	return vis[T] == Cnt;
}

int Dicnic(int x,int a)
{
	if (x == T || !a) return a;
	int flow = 0;
	for (int &i = cur[x]; i < v[x].size(); i++) {
		E &e = edgs[v[x][i]];
		if (e.cap == e.flow) continue;
		if (L[e.to] != L[x] + 1) continue;
		int f = Dicnic(e.to,min(a,e.cap - e.flow));
		if (!f) continue;
		e.flow += f;
		edgs[v[x][i]^1].flow -= f;
		a -= f;
		flow += f;
		if (!a) return flow;
	}
	if (!flow) L[x] = -1;
	return flow;
}

void DFS(int x)
{
	dfn[x] = low[x] = ++dfs_clock;
	s.push(x);
	for (int i = 0; i < v[x].size(); i++) {
		//if (v[x][i]&1) continue;
		E e = edgs[v[x][i]];
		if (e.cap == e.flow) continue;
		if (!dfn[e.to]) DFS(e.to),low[x] = min(low[x],low[e.to]);
		else if (!belong[e.to]) low[x] = min(low[x],low[e.to]);
	}
	if (low[x] >= dfn[x]) {
		++scc_num;
		for (;;) {
			int now = s.top(); s.pop();
			belong[now] = scc_num;
			if (now == x) break;
		}
	}
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	cin >> n >> m >> S >> T;
	for (int i = 1; i <= m; i++) {
		int x,y,w; scanf("%d%d%d",&x,&y,&w);
		Add(x,y,w);
	}
	int MaxFlow = 0;
	while (BFS()) {
		for (int i = 1; i <= n; i++) cur[i] = 0;
		MaxFlow += Dicnic(S,INF);
	}
	for (int i = 1; i <= n; i++) 
		if (!belong[i]) DFS(i);
	for (int i = 1; i <= n; i++)
		for (int j = 0; j < v[i].size(); j++) {
			if (v[i][j] & 1) continue;
			int Num = v[i][j]/2 + 1;
			E e = edgs[v[i][j]];
			if (e.cap != e.flow) continue;
			if (belong[i] != belong[e.to]) ans1[Num] = 1;
			if (belong[i] == belong[S] && belong[e.to] == belong[T]) ans2[Num] = 1;
		} 
	for (int i = 1; i <= m; i++) printf("%d %d\n",ans1[i],ans2[i]);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,我似乎没有找到与彩旗飘飘Python相关的引用内容。但是,如果你想在Python中实现彩旗飘飘的效果,你可以使用Pygame库来创建动画。以下是一个简单的示例代码,可以帮助你开始: ```python import pygame import random # 初始化pygame pygame.init() # 设置屏幕尺寸 screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("彩旗飘飘") # 加载彩旗图片 flag_image = pygame.image.load("flag.png") # 获取彩旗图片的宽度和高度 flag_width = flag_image.get_width() flag_height = flag_image.get_height() # 设置彩旗的起始位置 flag_x = screen_width // 2 - flag_width // 2 flag_y = screen_height // 2 - flag_height // 2 # 设置彩旗的速度 flag_speed_x = random.randint(-5, 5) flag_speed_y = random.randint(-5, 5) # 游戏主循环 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 移动彩旗 flag_x += flag_speed_x flag_y += flag_speed_y # 如果彩旗碰到边界,反转速度方向 if flag_x <= 0 or flag_x + flag_width >= screen_width: flag_speed_x = -flag_speed_x if flag_y <= 0 or flag_y + flag_height >= screen_height: flag_speed_y = -flag_speed_y # 清空屏幕 screen.fill((255, 255, 255)) # 绘制彩旗 screen.blit(flag_image, (flag_x, flag_y)) # 更新屏幕 pygame.display.flip() # 退出游戏 pygame.quit() ``` 请注意,你需要将代码中的"flag.png"替换为你自己的彩旗图片。此外,你还可以根据需要调整彩旗的起始位置、速度等参数。希望这个示例代码能帮助到你!<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [3d max制作彩旗飘飘](https://download.csdn.net/download/m0_71585230/88011500)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [[Rqnoj-371][AHOI1997]彩旗飘飘](https://blog.csdn.net/w745241408/article/details/7176600)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值