hdu3572 水题最大流 非递归dinic算法

Task Schedule

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4624    Accepted Submission(s): 1516

Problem Description

Our geometry princess XMM has stoped her study in computational geometry to concentrate on her newly opened factory. Her factory has introduced M new machines in order to process the coming N tasks. For the i-th task, the factory has to start processing it at or after day Si, process it for Pi days, and finish the task before or at day Ei. A machine can only work on one task at a time, and each task can be processed by at most one machine at a time. However, a task can be interrupted and processed on different machines on different days. 
Now she wonders whether he has a feasible schedule to finish all the tasks in time. She turns to you for help.

Input

On the first line comes an integer T(T<=20), indicating the number of test cases.

You are given two integer N(N<=500) and M(M<=200) on the first line of each test case. Then on each of next N lines are three integers Pi, Si and Ei (1<=Pi, Si, Ei<=500), which have the meaning described in the description. It is guaranteed that in a feasible schedule every task that can be finished will be done before or at its end day.

Output

For each test case, print “Case x: ” first, where x is the case number. If there exists a feasible schedule to finish all the tasks, print “Yes”, otherwise print “No”.

Print a blank line after each test case.

Sample Input

2
4 3
1 3 5
1 1 4
2 3 7
3 5 9

2 2
2 1 3
1 2 2

Sample Output

Case 1: Yes

Case 2: Yes

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<climits>
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
using namespace std;
struct Edge
{
    int s,t,f,next;
}edge[1100000];
int head[1010];
int cur[1010];
int pre[1010];
int stack[1100000];
int ent;
int n,m,times,s,t;
void add(int start,int last,int f)
{
    edge[ent].s=start;edge[ent].t=last;edge[ent].f=f;edge[ent].next=head[start];head[start]=ent++;
    edge[ent].s=last;edge[ent].t=start;edge[ent].f=0;edge[ent].next=head[last];head[last]=ent++;
}
bool bfs(int S,int T)
{
    memset(pre,-1,sizeof(pre));
    pre[S]=0;
    queue<int>q;
    q.push(S);
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
        for(int i=head[temp];i!=-1;i=edge[i].next)
        {
            int temp2=edge[i].t;
            if(pre[temp2]==-1&&edge[i].f)
            {
                pre[temp2]=pre[temp]+1;
                q.push(temp2);
            }
        }
    }
    return pre[T]!=-1;
}
int dinic(int start,int last)
{
    int flow=0,now;
    while(bfs(start,last))
    {
        int top=0;
        memcpy(cur,head,sizeof(head));
        int u=start;
        while(1)
        {
            if(u==last)//如果找到终点结束对中间路径进行处理并计算出该流
            {
                int minn=INT_MAX;
                for(int i=0;i<top;i++)
                {
                    if(minn>edge[stack[i]].f)
                    {
                        minn=edge[stack[i]].f;
                        now=i;
                    }
                }
                flow+=minn;
                for(int i=0;i<top;i++)
                {
                    edge[stack[i]].f-=minn;
                    edge[stack[i]^1].f+=minn;
                }
                top=now;
                u=edge[stack[top]].s;
            }
            for(int i=cur[u];i!=-1;cur[u]=i=edge[i].next)//找出从u点出发能到的边
                if(edge[i].f&&pre[edge[i].t]==pre[u]+1)
                    break;
            if(cur[u]==-1)//如果从该点未找到可行边,将该点标记并回溯
            {
                if(top==0)break;
                pre[u]=-1;
                u=edge[stack[--top]].s;
            }
            else//如果找到了继续运行
            {
                stack[top++]=cur[u];
                u=edge[cur[u]].t;
            }
        }
    }
    return flow;
}
int main()
{
    scanf("%d",&times);
    for(int cas=1;cas<=times;cas++)
    {
        ent=0;
        memset(head,-1,sizeof(head));
        int st,ed,lt;
        int maxn=0;
        int sum=0;
        s=0;t=1001;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d",&lt,&st,&ed);
            maxn=Max(maxn,ed);
            for(int j=st;j<=ed;j++)
                add(j,i+500,1);
            add(i+500,t,lt);
            sum+=lt;
        }
        for(int i=1;i<=maxn;i++)
            add(s,i,m);
        if(dinic(s,t)==sum)printf("Case %d: Yes\n",cas);
        else printf("Case %d: No\n",cas);
        printf("\n");
    }
    return 0;
}
时间: 2024-10-11 01:27:07

hdu3572 水题最大流 非递归dinic算法的相关文章

二叉树的中序、先序、后序遍历非递归遍历算法(使用堆栈,用循环实现)

1 typedef struct TreeNode *BinTree; 2 typedef BinTree Position; 3 struct TreeNode{ 4 ElementType Data; 5 BinTree Left; 6 BinTree Right; 7 }; 8 BinTree BT; 9 void InOrderTraversal(BinTree BT)//中序遍历非递归遍历算法(使用堆栈,用循环实现) 10 { 11 BinTree T=BT; 12 Stack S=C

先序遍历的非递归遍历算法

先序遍历的非递归遍历算法: 1 void InOrderTraversal(BinTree BT) 2 { 3 BinTree T=BT; 4 stack S=CreatStack(MaxSize)://创建并初始化堆栈S 5 while(T || !IsEmpty(S)){ 6 While(T){//一直向左并将沿途节点压入堆栈 7 printf("%5d",T->Data);//(访问)打印节点 8 Push(S,T); 9 T=T->left; 10 } 11 if(

每天刷个算法题20160518:非递归二叉树遍历

版权所有.所有权利保留. 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/51502254 为了防止思维僵化,每天刷个算法题.已经刷了几天了,现在贴点代码. 2002年我初中二年级,开始学习BASIC语言.2004年中考之后,开始参加NOIP,系统学习算法.一直非常喜欢算法,但工作后几乎不再碰这些东西.现在准备重新捡起来. 我已经建了一个开源项目,每天的题目都在里面: https://github.com/Xiaofe

借树的遍历一题,尝试非递归三种遍历

#include <iostream> #include <cstdio> #include <cstdlib> #include <stack> using namespace std; int z[30],h[30]; class Tree { public: int data; Tree *lchild; Tree *rchild; Tree() { lchild=NULL; rchild=NULL; } }*root; Tree *CreatNode

hiho一下 第119周 #1398 : 网络流五&#183;最大权闭合子图 【最小割-最大流--Ford-Fulkerson 与 Dinic 算法】

#1398 : 网络流五·最大权闭合子图 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 周末,小Hi和小Ho所在的班级决定举行一些班级建设活动. 根据周内的调查结果,小Hi和小Ho一共列出了N项不同的活动(编号1..N),第i项活动能够产生a[i]的活跃值. 班级一共有M名学生(编号1..M),邀请编号为i的同学来参加班级建设活动需要消耗b[i]的活跃值. 每项活动都需要某些学生在场才能够进行,若其中有任意一个学生没有被邀请,这项活动就没有办法进行. 班级建设的活

二叉树遍历(递归与非递归)算法

二叉树定义 #include<stdio.h> #include<malloc.h> #define STACK_INIT_SIZE 30 #define STACKINCREMENT 10 #define OK 1 #define ERROR -1 typedef char TElemType; typedef struct BiTNode{ TElemType data; struct BiTNode *lchild, *rchild; int flag; }BiTNode,*

二叉树,递归非递归遍历算法(全)

包含了所有的非递归和递归的算法: #include<iostream> #include<queue> #include<stack> using namespace std; //二叉树结点的描述 typedef struct BiTNode { char data; struct BiTNode *lchild, *rchild; //左右孩子 }BiTNode,*BiTree; //按先序遍历创建二叉树 //BiTree *CreateBiTree() //返回结

[算法]二叉树的非递归遍历算法

1.二叉树的非递归中序遍历算法 二叉树的中序遍历方法是:左中右,因此一开始会顺着根节点的左孩子一直往下(这点和先序遍历一样,这也是二者前面部分代码很相似的原因),到最后一个左孩子时尝试把它的右孩子塞进栈内,然后顺着它的的左孩子而下,直到不能访问为止.利用的栈FILO的特性,对每个节点都进行顺左孩子而下即可. 上代码: 1 void inOrder(TreeNode* root,vector<int>& inOrder) 2 { 3 stack<TreeNode*>st; 4

leetCode -- Binary Tree的3个水题 —— 3种非Recursive遍历

Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? ''' Created on