BZOJ1497【最大权闭合图】

/* I will wait for you */

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <string>
#define make make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const int maxn = 200010;
const int maxm = 1010;
const int maxs = 26;
const int inf = 0x3f3f3f3f;
const int P = 1000000007;
const double error = 1e-9;

inline int read()
{
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9')
		  f = (ch == '-' ? -1 : 1), ch = getchar();
	while (ch >= '0' && ch <= '9')
	 	  x = x * 10 + ch - '0', ch = getchar();
	return x * f;
}

struct edge
{
	int v, a, next;
} e[maxn];

int n, m, s, t, cnt, ans, 
	head[maxn], cur[maxn], dis[maxn], q[maxn];

void insert(int u, int v, int a)
{
	e[cnt] = (edge) {v, a, head[u]}, head[u] = cnt++;
	e[cnt] = (edge) {u, 0, head[v]}, head[v] = cnt++;
}

int bfs()
{
	memset(dis, -1, sizeof dis);
	
	int he = 0, ta = 1;
	q[0] = s, dis[s] = 0;
	
	while (he != ta) {
		int u = q[he++];
		for (int i = head[u]; i != -1; i = e[i].next) {
			int v = e[i].v;
			if (e[i].a && dis[v] == -1)
				dis[v] = dis[u] + 1, q[ta++] = v;
		}
	}
	
	return dis[t] != -1;
}

int dfs(int u, int a)
{
	if (u == t) return a;
	
	int f, flow = 0;
	
	for (int i = cur[u]; a && i != -1 ;i = e[i].next) {
		int v = e[i].v;
		if (e[i].a && dis[v] == dis[u] + 1) {
			f = dfs(v, min(a, e[i].a));
			e[i].a -= f, e[i ^ 1].a += f;
			a -= f, flow += f;
			if (e[i].a) cur[u] = i;
		}
	}
	
	if (!flow) dis[u] = -1;
	return flow;
}

int dinic()
{
	while (bfs()) {
		memcpy(cur, head, sizeof head);
		ans -= dfs(s, inf);
	}
}

int main()
{
	n = read(), m = read();
	s = n + m + 1, t = n + m + 2;
	
	memset(head, -1, sizeof head);
	
	for (int i = 1; i <= n; i++) {
		int w = read();
		insert(i, t, w);
	}
	
	for (int i = 1; i <= m; i++) {
		int u = read(), v = read(), w = read();
		insert(n + i, u, inf), insert(n + i, v, inf);
		insert(s, n + i, w), ans += w;
	}

	dinic(), printf("%d\n", ans);	

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BZOJ 2908 题目是一个数据下载任务。这个任务要求下载指定的数据文件,并统计文件中小于等于给定整数的数字个数。 为了完成这个任务,首先需要选择一个合适的网址来下载文件。我们可以使用一个网络爬虫库,如Python中的Requests库,来帮助我们完成文件下载的操作。 首先,我们需要使用Requests库中的get()方法来访问目标网址,并将目标文件下载到我们的本地计算机中。可以使用以下代码实现文件下载: ```python import requests url = '目标文件的网址' response = requests.get(url) with open('本地保存文件的路径', 'wb') as file: file.write(response.content) ``` 下载完成后,我们可以使用Python内置的open()函数打开已下载的文件,并按行读取文件内容。可以使用以下代码实现文件内容读取: ```python count = 0 with open('本地保存文件的路径', 'r') as file: for line in file: # 在这里实现对每一行数据的判断 # 如果小于等于给定整数,count 加 1 # 否则,不进行任何操作 ``` 在每一行的处理过程中,我们可以使用split()方法将一行数据分割成多个字符串,并使用int()函数将其转换为整数。然后,我们可以将该整数与给定整数进行比较,以判断是否小于等于给定整数。 最后,我们可以将统计结果打印出来,以满足题目的要求。 综上所述,以上是关于解决 BZOJ 2908 数据下载任务的简要步骤和代码实现。 希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值