poj1698--最大流(Dinic)

题目大意:

爱丽丝要拍电影,有n部电影,规定爱丽丝每天只能拍一部电影,每部电影在每个礼拜只有固定的几天可以拍电影,只可以拍前面w个礼拜,并且这部电影要拍d天,问爱丽丝能不能拍完所有的电影。

思路:

建图。点1~350代表天数(因为最多只有350天),点351~370代表电影(最多只有20部电影)。从源点向每部电影连一条容量为d的边,从每部电影向可以拍摄它的那几天连一条容量为1的边,从每天向汇点连一条容量为1的边(限制每天只能拍一部电影)。再求一遍最大流,看其是否与sum(d[i])相等,如果是,就输出Yes,否则输出No。

具体看代码。

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
#define INF 2147483647
vector<int>g[400];
queue<int>q;
struct Edge{
    int to,from,cap,flow;
}edge[160000];
int i,j,k,s,t,n,m,x,y,sum,w,d,cur[400],T,dist[400];
bool a[8],vis[400];
void addedge(int x,int y,int cap){
    edge[m++]=(Edge){y,x,cap,0};
    edge[m++]=(Edge){x,y,0,0};
    g[x].push_back(m-2);
    g[y].push_back(m-1);
}
bool bfs(){
    memset(vis,0,sizeof(vis));
    vis[s]=1;
    dist[s]=0;
    q.push(s);
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=0;i<g[x].size();i++){
            Edge& e=edge[g[x][i]];
            if(e.cap>e.flow&&!vis[e.to]){
                vis[e.to]=1;
                dist[e.to]=dist[x]+1;
                q.push(e.to);
            }
        }
    }
    return vis[t];
}
int dfs(int x,int a){
    if(x==t||!a)return a;
    int flow=0,f;
    for(int& i=cur[x];i<g[x].size();i++){
        Edge& e=edge[g[x][i]];
        if(dist[x]+1==dist[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))){
            e.flow+=f;
            edge[g[x][i]^1].flow-=f;
            flow+=f;
            a-=f;
            if(!a)break;
        }
    }
    return flow;
}
int maxflow(){
    int flow=0;
    while(bfs()){
        memset(cur,0,sizeof(cur));
        flow+=dfs(s,INF);
    }
    return flow;
}
int main()
{
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        sum=m=0;t=351+n;
        for(i=1;i<=n;i++){
            for(j=1;j<=7;j++)scanf("%d",&a[j]);
            scanf("%d%d",&d,&w);
            sum+=d;
            addedge(s,i+350,d);
            for(j=1;j<=7;j++)
            if(a[j])for(k=0;k<w;k++)addedge(i+350,k*7+j,1);
        }
        for(i=1;i<=350;i++)addedge(i,t,1);
        if(maxflow()==sum)printf("Yes\n");else printf("No\n");
        for(i=0;i<=t;i++)g[i].clear();
    }
    return 0;
}

poj1698

时间: 2024-07-30 16:29:59

poj1698--最大流(Dinic)的相关文章

最大流 Dinic + Sap 模板

不说别的,直接上模板. Dinic+当前弧优化: struct Edge{ int x,y,c,ne; }e[M*2]; int be[N],all; int d[N],q[N]; int stack[N],top;//栈存的是边 int cur[N];//当前弧优化 void add(int x, int y, int z)//需保证相反边第一个为偶数 { e[all].x=x; e[all].y=y; e[all].c=z; e[all].ne=be[x]; be[x]=all++; e[a

【算法】网络最大流 Dinic

Dinic的大体思路是和EK差不多的(其实很多算法的大体思路都一样),只不过Dinic在每次寻找增广路时先bfs一下,给每个点都加上一个等级,而规定:只有等级相邻的两个点之间才能走,那么在dfs时就会减掉很多无用因此不必要的道路 1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdio> 5 #include<queue> 6 using na

POJ训练计划1459_Power Network(网络流最大流/Dinic)

解题报告 这题建模实在是好建,,,好贱,,, 给前向星给跪了,纯dinic的前向星竟然TLE,sad,,,回头看看优化,,, 矩阵跑过了,2A,sad,,, /************************************************************************* > File Name: PowerN.cpp > Author: _nplus > Mail: [email protected] > Time: 2014年07月19日 星期

网络最大流 dinic算法

一句话题意:给出一个网络图,以及其源点和汇点,求出其网络最大流 //dinic算法; //时间复杂度O(V^2E); #include<bits/stdc++.h> #define inf 999999 #define maxn 200000 using namespace std; int n,m,s,t; int ans=0; struct Edge { int to,next,w; }; struct Edge edge[maxn]; int head[maxn],val[maxn],p

poj-1459-最大流dinic+链式前向星

title: poj-1459-最大流dinic+链式前向星 date: 2018-11-22 20:57:54 tags: acm 刷题 categories: ACM-网络流-最大流 概述 这道是一道网络流里最大流的板子题,,, 暑期集训网络流草草水过,,连基本的算法都不知道有哪些,,,更别提怎么实现了,,,只知道网络流的大致的概念,, 今天花了一天的时间重新学习了一波,,,本以为这东西很简单,,,没想到不仅算法的实现一大堆的东西,,就连题目都有时候看不懂,,,,感受就是网络流的题不仅算法实

POJ 3281 Dining 最大流 Dinic算法

Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10768   Accepted: 4938 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John has cooked fabulo

POJ 1149 PIGS (网络最大流 Dinic 建对图你就赢了)

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17598   Accepted: 7977 Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come t

【uva 753】A Plug for UNIX(图论--最大流 Dinic)

题意:有N个插头,M个设备和K种转换器.要求插的设备尽量多,问最少剩几个不匹配的设备. 解法:给读入的各种插头编个号,源点到设备.设备通过转换器到插头.插头到汇点各自建一条容量为1的边.跑一次最大流就可得到最多匹配的设备数. 注意啊,我这个代码在神奇?的地方死循环了——判断队列为空的语句......Σ( ° △ °|||)︴所以大家主要看“行文思路” ? :-) 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring

POJ 3281 Dining(最大流dinic&amp;&amp;SAP)

Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although

POJ2112_Optimal Milking(网洛流最大流Dinic+最短路Flody+二分)

解题报告 农场有k个挤奶机和c头牛,每头牛到每一台挤奶机距离不一样,每台挤奶机每天最多挤m头牛的奶. 寻找一个方案,安排每头牛到某一挤奶机挤奶,使得c头牛须要走的全部路程中的最大路程的最小值. 要使每一头牛都去挤奶,那么建完模型就要推断是否满流. 因为是多源多点的网络,如果源点0,汇点n+1(n=k+c) 源点到每一头牛的容量为1,每一台机器到汇点的容量为m;用flody求出随意一头牛到随意一台机器的最短路; 对于取最大距离的最小值能够用二分来找. #include <iostream> #i