LA 2531 The K-league 最大流
- #include <iostream>
- #include <cstdio>
- #include <fstream>
- #include <algorithm>
- #include <cmath>
- #include <deque>
- #include <vector>
- #include <queue>
- #include <string>
- #include <cstring>
- #include <map>
- #include <stack>
- #include <set>
- #define INF 0x3f3f3f3f
- #define OPEN_FILE
- #define MAXN 626
- using namespace std;
- int n;
- int win[MAXN], remain[MAXN][MAXN];
- struct Edge{
- int from, to, cap, flow;
- //Edge(int u, int v, int c, int f) :from(u), to(v), cap(c), flow(f){};
- };
- bool comp(const Edge& a, const Edge& b){
- return (a.from < b.from || (a.from == b.from && a.to < b.to));
- }
- struct Dinic{
- int n, m, i, s, t;
- Edge e;
- vector<Edge> edges;
- vector<int> G[MAXN];
- int d[MAXN], cur[MAXN];
- bool vis[MAXN];
- void init(int n){
- this->n = n;
- for (i = ; i <= n; i++){
- G[i].clear();
- }
- edges.clear();
- }
- void AddEdge(int from, int to, int cap){
- edges.push_back(Edge{ from, to, cap, });
- edges.push_back(Edge{ to, from, , });
- m = edges.size();
- G[from].push_back(m - );
- G[to].push_back(m - );
- }
- bool BFS(){
- memset(vis, , sizeof(vis));
- queue<int> Q;
- Q.push(s);
- d[s] = ;
- vis[s] = ;
- while (!Q.empty()){
- int x = Q.front();
- Q.pop();
- for (i = ; i < G[x].size(); i++){
- Edge& e = edges[G[x][i]];
- if (!vis[e.to] && e.cap > e.flow){
- vis[e.to] = true;
- d[e.to] = d[x] + ;
- Q.push(e.to);
- }
- }
- }
- return vis[t];
- }
- int DFS(int x, int a){
- if (x == t || a == ) return a;
- int flow = , f;
- for (int& i = cur[x]; i < G[x].size(); i++){
- Edge& e = edges[G[x][i]];
- if (d[x] + == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > ){
- e.flow += f;
- edges[G[x][i] ^ ].flow -= f;
- flow += f;
- a -= f;
- if (a == ) break;
- }
- }
- return flow;
- }
- int MaxFlow(int s, int t, int need){
- int flow = ;
- this->s = s;
- this->t = t;
- while (BFS()){
- memset(cur, , sizeof(cur));
- flow += DFS(s, INF);
- if (flow > need) return flow;
- }
- return flow;
- }
- bool checkFull(int s){
- for (int i = ; i < G[s].size(); i++){
- if (edges[G[s][i]].flow != edges[G[s][i]].cap){
- return false;
- }
- }
- return true;
- }
- };
- int main()
- {
- #ifdef OPEN_FILE
- freopen("in.txt", "r", stdin);
- freopen("out.txt", "w", stdout);
- #endif // OPEN_FILE
- int T, x;
- scanf("%d", &T);
- for (int cas = ; cas <= T; cas++){
- scanf("%d", &n);
- memset(win, , sizeof(win));
- for (int i = ; i <= n; i++){
- scanf("%d%d", &win[i], &x);
- }
- memset(remain, , sizeof(remain));
- int p = ;
- for (int i = ; i <= n; i++){
- for (int j = ; j <= n; j++){
- scanf("%d", &x);
- if (i == j) continue;
- remain[i][] += x;
- if (remain[i][j] == && remain[j][i] == ){
- remain[i][j] = x;
- ++p;
- }
- }
- }
- int s = , t = n + p + , q;
- bool flag, first;
- Dinic ex;
- first = false;
- for (int k = ; k <= n; k++){
- ex.init(n * n);
- flag = false;
- q = ;
- int total = win[k] + remain[k][];
- for (int i = ; i <= n; i++){
- for (int j = i + ; j <= n; j++){
- if (!remain[i][j]) continue;
- ex.AddEdge(s, q, remain[i][j]);
- ex.AddEdge(q, p + i, INF);
- ex.AddEdge(q, p + j, INF);
- q++;
- }
- if (total - win[i] < ) {
- flag = true;
- break;
- }
- ex.AddEdge(p + i, t, total - win[i]);
- }
- if (flag){
- continue;
- }
- ex.MaxFlow(s, t, INF);
- if (ex.checkFull()){
- if (first){
- printf(" ");
- }
- printf("%d", k);
- first = true;
- }
- }
- printf("\n");
- }
- }
LA 2531 The K-league 最大流的更多相关文章
- POJ - 2516 Minimum Cost 每次要跑K次费用流
传送门:poj.org/problem?id=2516 题意: 有m个仓库,n个买家,k个商品,每个仓库运送不同商品到不同买家的路费是不同的.问为了满足不同买家的订单的最小的花费. 思路: 设立一个源 ...
- poj-2516.minimum cost(k次费用流)
Minimum Cost Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19883 Accepted: 7055 Des ...
- (算法入门经典大赛 优先级队列)LA 3135(之前K说明)
A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor da ...
- hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙
/** 题目:hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4106 ...
- 【wikioi】1034 家园(最大流+特殊的技巧)
http://wikioi.com/problem/1034/ 太神了这题. 其实一开始我以为是费用流,但是总感觉不对. 原因是我没看到一句话,特定的时刻到达特定的点!! 也就是说,并不是每艘船每次都 ...
- BZOJ 2324 营救皮卡丘(最小费用最大流)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2324 题意:n+1个城市(0到n).初始时K个 人都在0城市.城市之间有距离.要求(1) ...
- hdu3081 Marriage Match II(二分+并查集+最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意: n个女生与n个男生配对,每个女生只能配对某些男生,有些女生相互是朋友,每个女生也可以跟她 ...
- POJ 2516 Minimum Cost (费用流)
题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...
- 【费用流】【CODEVS】1227 方格取数2
[算法]最小费用最大流(费用流) [题解] 费用流:http://www.cnblogs.com/onioncyc/p/6496532.html 本题构图: 在有限的k次行走中尽可能多的拿到数字,明显 ...
随机推荐
- CUDA中的归约
CUDA编程实战书中的乘方和解决办法: 对一个数组执行某种计算,然后产生一个更小的结果数组. 由一个线程在共享内存上进行迭代并计算出总和值.而如果用并行,所花时间就与数组长度的对数成正比. 代码的思想 ...
- DNS Prefetching
For Developers > Design Documents > DNS Prefetching 目录 1 Problem 2 Solution 3 Architectur ...
- ArcGIS api for javascript——地理编码任务-地理编码地址
描述 本例允许用户输入一个地址,然后显示匹配的地址的位置.这通常地被称为地理编码.在ArcGIS JavaScript API中,使用Locator类执行地理编码. 定位器构造函数需要ArcGIS S ...
- c3p0出现 An attempt by a client to checkout a Connection has timed out
java.sql.SQLException: An attempt by a client to checkout a Connection has timed out. at com.mchange ...
- Codeforces 327A-Flipping Game(暴力枚举)
A. Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Python使用Redis实现一个简单作业调度系统
Python使用Redis实现一个简单作业调度系统 概述 Redis作为内存数据库的一个典型代表,已经在非常多应用场景中被使用,这里仅就Redis的pub/sub功能来说说如何通过此功能来实现一个简单 ...
- 【LeetCode】3Sum Closest 解题报告
[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...
- UINavigationBar的系统渲染方式
昨天想手工实现一下类知乎日报的Navigation Bar的动态颜色改变,但不管怎么设置Navigation Bar的 backgroundColor barTintColor alpha參数都达不到 ...
- vue.2.0-自定义全局组件
App.vue <template> <div id="app"> <h3>welcome vue-loading</h3> < ...
- 10.MongoDB:将Json数据直接写入MongoDB的方法
Json转Bson MongoDB中是以Bson数据格式进行存储的,Json字符串没有办法直接写入MongoDB 可以将Json字符串转换成DBObject或者Document,然后写入MongoDB ...