POJ 1698 Alice's Chance(最大流+拆点)

POJ 1698 Alice‘s Chance

题目链接

题意:拍n部电影。每部电影要在前w星期完毕,而且一周仅仅有一些天是能够拍的,每部电影有个须要的总时间,问能否拍完电影

思路:源点向每部电影连边,容量为d,然后每部电影相应能拍的那天连边,因为每天容量限制是1。所以进行拆点,然后连向汇点就可以

代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

const int MAXNODE = 1005;
const int MAXEDGE = 100005;

typedef int Type;
const Type INF = 0x3f3f3f3f;

struct Edge {
	int u, v;
	Type cap, flow;
	Edge() {}
	Edge(int u, int v, Type cap, Type flow) {
		this->u = u;
		this->v = v;
		this->cap = cap;
		this->flow = flow;
	}
};

struct Dinic {
	int n, m, s, t;
	Edge edges[MAXEDGE];
	int first[MAXNODE];
	int next[MAXEDGE];
	bool vis[MAXNODE];
	Type d[MAXNODE];
	int cur[MAXNODE];
	vector<int> cut;

	void init(int n) {
		this->n = n;
		memset(first, -1, sizeof(first));
		m = 0;
	}
	void add_Edge(int u, int v, Type cap) {
		edges[m] = Edge(u, v, cap, 0);
		next[m] = first[u];
		first[u] = m++;
		edges[m] = Edge(v, u, 0, 0);
		next[m] = first[v];
		first[v] = m++;
	}

	bool bfs() {
		memset(vis, false, sizeof(vis));
		queue<int> Q;
		Q.push(s);
		d[s] = 0;
		vis[s] = true;
		while (!Q.empty()) {
			int u = Q.front(); Q.pop();
			for (int i = first[u]; i != -1; i = next[i]) {
				Edge& e = edges[i];
				if (!vis[e.v] && e.cap > e.flow) {
					vis[e.v] = true;
					d[e.v] = d[u] + 1;
					Q.push(e.v);
				}
			}
		}
		return vis[t];
	}

	Type dfs(int u, Type a) {
		if (u == t || a == 0) return a;
		Type flow = 0, f;
		for (int &i = cur[u]; i != -1; i = next[i]) {
			Edge& e = edges[i];
			if (d[u] + 1 == d[e.v] && (f = dfs(e.v, min(a, e.cap - e.flow))) > 0) {
				e.flow += f;
				edges[i^1].flow -= f;
				flow += f;
				a -= f;
				if (a == 0) break;
			}
		}
		return flow;
	}

	Type Maxflow(int s, int t) {
		this->s = s; this->t = t;
		Type flow = 0;
		while (bfs()) {
			for (int i = 0; i < n; i++)
				cur[i] = first[i];
			flow += dfs(s, INF);
		}
		return flow;
	}

	void MinCut() {
		cut.clear();
		for (int i = 0; i < m; i += 2) {
			if (vis[edges[i].u] && !vis[edges[i].v])
				cut.push_back(i);
		}
	}
} gao;

int t, n, day[10], d, w, vis[400];

int main() {
	scanf("%d", &t);
	while (t--) {
		memset(vis, 0, sizeof(vis));
		gao.init(1000);
		scanf("%d", &n);
		int sum = 0;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= 7; j++)
				scanf("%d", &day[j]);
			scanf("%d%d", &d, &w);
			sum += d;
			gao.add_Edge(0, i, d);
			for (int j = 0; j < w; j++) {
				for (int k = 1; k <= 7; k++) {
					if (day[k]) {
						gao.add_Edge(i, 20 + j * 7 + k, INF);
						vis[20 + j * 7 + k] = 1;
					}
				}
			}
		}
		for (int i = 21; i <= 370; i++) {
			if (!vis[i]) continue;
			gao.add_Edge(i, i + 350, 1);
			gao.add_Edge(i + 350, 1000, INF);
		}
		printf("%s\n", gao.Maxflow(0, 1000) == sum ? "Yes" : "No");
	}
	return 0;
}

POJ 1698 Alice's Chance(最大流+拆点)

时间: 2024-08-05 00:27:33

POJ 1698 Alice&#39;s Chance(最大流+拆点)的相关文章

poj 1698 Alice&#39;s Chance SAP 最大流

[题意]:Alice有n部电影要拍,规定爱丽丝每部电影在每个礼拜只有固定的几天可以拍电影,只可以拍前面w个礼拜,并且这部电影要拍d天,问爱丽丝能不能拍完所有的电影 [建图]:源点与每部电影连边,容量为天数,每部电影与可以拍该电影的那些天数连边,容量为1,再所有的天数与汇点连边容量为1. 要注意天数和汇点连边的时候不要重复了,我这里用的数组不会出现这种情况. 1 #include<iostream> 2 #include<vector> 3 #include<cstring&g

poj 1698 Alice&#39;s Chance 拆点最大流

将星期拆点,符合条件的连边,最后统计汇点流量是否满就行了,注意结点编号. #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> #include<map> #include<queue> #include<vector> #include<s

POJ 1698 Alice&#39;s Chance(最大流+拆点)

POJ 1698 Alice's Chance 题目链接 题意:拍n部电影,每部电影要在前w星期完成,并且一周只有一些天是可以拍的,每部电影有个需要的总时间,问是否能拍完电影 思路:源点向每部电影连边,容量为d,然后每部电影对应能拍的那天连边,由于每天容量限制是1,所以进行拆点,然后连向汇点即可 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm> usi

POJ 1698 Alice&#39;s Chance(网络流之最大流)

题目地址:POJ 1698 水题..将每部电影与它可以演的那一天连边就行了.建二分图.用二分最大匹配也完全可以做. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queu

poj 1698 Alice&#39;s Chance(网络流)

Alice's Chance Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5280   Accepted: 2171 Description Alice, a charming girl, have been dreaming of being a movie star for long. Her chances will come now, for several filmmaking companies invit

POJ 1698 Alice&#39;s Chance 网络流(水

题目链接:点击打开链接 题目大意:   有个人想拍n部电影,每部电影限定每周哪几天可以拍 并且必须在第ki周之前把这部电影拍完,问能否拍完n部电影 解题思路:  把每部电影当作一个顶点,源点指向这些顶点,容量为该电影需要拍多少天 然后把每一天都当作顶点,某个工作可以在这天完成就连容量为1大边 每天的顶点指向汇点,容量也为1 最后求出最大流,满流则说明可以完成这些工作 啦啦啦 #include <cstdio> #include <cstring> #include <algo

POJ 1698 Alice&#39;s Chance(网络流+构图)

题目链接:http://poj.org/problem?id=1698 题目: Description Alice, a charming girl, have been dreaming of being a movie star for long. Her chances will come now, for several filmmaking companies invite her to play the chief role in their new films. Unfortuna

poj 1698 Alice&#39;s Chance 二分图多重匹配

题意: 一个演员要拍n部电影,每部电影只能在一周的特定几天拍(如周2,周4,周5),第i部电影要拍di天,必须要在wi周拍完,问演员是否可以完成任务. 分析: 建二分图,转化为二分图的多重匹配. 代码: //poj 1698 //sep9 #include <iostream> using namespace std; const int maxX=64*7; const int maxY=64; int g[maxX][maxY],match[maxY][maxX]; int vis[max

Alice&amp;#39;s Chance

id=1698" style="background-color:rgb(51,255,51)">主题链接 意甲冠军: 爱丽丝要拍电影.有n部电影,规定爱丽丝第i部电影在每一个礼拜仅仅有固定的几天能够拍电影,且仅仅能在前wi个周拍,而且这部电影要拍di天才干结束.问爱丽丝能不能拍全然部的电影 第一行代表有多少组数据,对于每组数据第一行代表有n部电影,接下来2到n+1行,每行代表一个电影,每行9个数,前面7个数,1代表拍.0代表不拍,第8个数代表要拍几天,第9个数代表有几