Annual Congress of MUD(最大流)

Annual Congress of MUD

时间限制: 1 Sec  内存限制: 128 MB
提交: 80  解决: 10
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Multiuser dungeon games, also called MUD games, are real-time virtual-world multiplayer games that are usually played online with text-based commands. The players do not meet normally, but every year, there is the Annual Congress of MUD (ACM) where MUD lovers all around the world could meet in person.
ACM is so popular that the event each year spans around 20 days. Each day, there will be a special gathering for MUD game designers to introduce their new games to the others. 
Each player will usually spend a few days on the ACM site, and in-between will be invited in exactly one day to join this special gathering.
This year, ACM is held at your city, and your boss is an organiser, and he wants to find a best way to assign the players to these special gatherings (one player to one special gathering within his or her duration of stay), so that the maximum number of players among all gatherings is minimized.
Your boss is an extremely impatient guy. He wants to have a system that can tell the maximum number of players among all gatherings, in any best assignment, after each player enters his or her duration of stay. Your task is to help your boss develop such a system.

输入

An instance of the problem consists of N + 1 lines. The first line specifies the integers N and D, seperated by a space. In the ith line of the following N lines, it contains two integers xi and yi , separated by a space. Note that the test data file may contain more than one instance. The last instance is followed by a line containing a single 0.

输出

For each instance, an integer i is marked if and only if the maximum number of players in the best assignment increases after the duration of stay [xi , yi ] of the ith player is keyed in. By default, 1 is always marked. The output of the corresponding instance is the list of all marked integers in ascending order, separated by a space between successive integers, followed by a newline character.

样例输入

3 3
1 1
1 1
1 1
3 3
1 2
2 3
1 1
3 3
1 2
1 2
1 2
0

样例输出

1 2 3
1
1 3
思路:将每个区间看作一个点,在源点s与输入的区间之间连一条流量为一的边,如果该区间已经存在,则将对应边的流量加一;在每一个区间与区间内每一个点之间建一条流量为无穷的边;在所有1~D的点与汇点t之间建一条流量为max的边,max为当前的the maximum number of players among all gatherings;当然max起始为0.

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 const int MAXN=305;
  4 const int INF=1e9+7;
  5 struct Max_flow
  6 {
  7     struct
  8     {
  9         int v,cap,next;
 10     } e[MAXN*MAXN];
 11     int cnt_edge,cur_clk,head[MAXN];
 12     void init(int cnt)
 13     {
 14         cnt_edge=0;
 15         memset(head,0xff,sizeof(int)*cnt);
 16     }
 17     int add_edge_(int u,int v,int cap)
 18     {
 19         e[cnt_edge]={v,cap,head[u]};
 20         head[u]=cnt_edge;
 21         return cnt_edge++;
 22     }
 23     int add_edge(int u,int v,int cap)
 24     {
 25         int res=add_edge_(u,v,cap);
 26         add_edge_(v,u,0);
 27         return res;
 28     }
 29     int clk[MAXN],pre[MAXN];
 30     queue<int> que;
 31     bool bfs(int s,int t)
 32     {
 33         while(!que.empty()) que.pop();
 34         cur_clk++;
 35         clk[s]=cur_clk;
 36         que.push(s);
 37         while(!que.empty())
 38         {
 39             int u=que.front();
 40             que.pop();
 41             for(int i=head[u]; i!=-1; i=e[i].next)
 42             {
 43                 if(e[i].cap>0)
 44                 {
 45                     int v=e[i].v;
 46                     if(clk[v]!=cur_clk)
 47                     {
 48                         pre[v]=i;
 49                         clk[v]=cur_clk;
 50                         if(v==t) return true;
 51                         que.push(v);
 52                     }
 53                 }
 54             }
 55         }
 56         return false;
 57     }
 58     int max_flow(int s,int t)
 59     {
 60         int flow=0;
 61         while(bfs(s,t))
 62         {
 63             int min_cap=INF;
 64             for(int u=t;u!=s;u=e[pre[u]^1].v)
 65             {
 66                 if(min_cap>e[pre[u]].cap) min_cap=e[pre[u]].cap;
 67             }
 68             for(int u=t; u!=s; u=e[pre[u]^1].v)
 69             {
 70                 e[pre[u]].cap-=min_cap;
 71                 e[pre[u]^1].cap+=min_cap;
 72             }
 73             flow+=min_cap;
 74         }
 75         return flow;
 76     }
 77 } G;
 78 int vis[30][30],idx[30][30],edg_t[30],s_edg[MAXN];
 79 int n,d,now,m,s,t,flow;
 80 bool flag;
 81 int main()
 82 {
 83     while(scanf("%d",&n)!=EOF && n)
 84     {
 85         now++,m=0,flow=0,flag=false;
 86         scanf("%d",&d);
 87         for(int i=0;i<d;i++)
 88         {
 89             for(int j=i;j<d;j++)
 90             {
 91                 idx[i][j]=m++;
 92             }
 93         }
 94         G.init(m+d+2);
 95         s=m+d,t=m+d+1;
 96         for(int i=0;i<d;i++) edg_t[i]=G.add_edge(m+i,t,0);
 97         for(int i=0;i<n;i++)
 98         {
 99             int x,y;
100             scanf("%d %d",&x,&y);
101             x--,y--;
102             int v=idx[x][y];
103             if(vis[x][y]!=now)
104             {
105                 vis[x][y]=now;
106                 for(int i=x;i<=y;i++) G.add_edge(v,m+i,INF);
107                 s_edg[v]=G.add_edge(s,v,1);
108             }
109             else
110             {
111                 G.e[s_edg[v]].cap++;
112             }
113             flow+=G.max_flow(s,t);
114             //cout<<"ni"<<"->"<<flow<<endl;
115             if(flow!=i+1)
116             {
117                 if(flag) printf(" ");
118                 else flag=true;
119                 printf("%d",i+1);
120                 for(int i=0;i<d;i++) G.e[edg_t[i]].cap++;
121             }
122         }
123         puts("");
124     }
125     return 0;
126 }

原文地址:https://www.cnblogs.com/lglh/p/9589201.html

时间: 2024-10-18 16:52:50

Annual Congress of MUD(最大流)的相关文章

Dr Wei Min Huang

 Curriculum Vitae Dr Wei Min Huang is currently an Associate Professor (tenured) at the School of Mechanical and Aerospace Engineering, Nanyang Technological University, Singapore. With over 20 years of experience on various shape memory materials (a

Ural1109_Conference(二分图最大匹配/匈牙利算法/网络最大流)

解题报告 二分图第一题. 题目描述: 为了参加即将召开的会议,A国派出M位代表,B国派出N位代表,(N,M<=1000) 会议召开前,选出K队代表,每对代表必须一个是A国的,一个是B国的; 要求每一个代表要与另一方的一个代表联系,除了可以直接联系,也可以电话联系,求电话联系最少 思路: 电话联系最少就要使直接联系最大,又是一一匹配关系,就是二分图的最大匹配. 下面是匈牙利算法. #include <cstdio> #include <cstring> #include <

HNU 13377 Book Club (最大流 判环)

Book Club Time Limit: 5000ms, Special Time Limit:12500ms, Memory Limit:65536KB Total submit users: 34, Accepted users: 16 Problem 13377 : No special judgement Problem description Porto's book club is buzzing with excitement for the annual book exchan

对IO流的操作(文件大小,拷贝,移动,删除)

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.SequenceInputStream; class LjyFileClass { /*LjyFileClass工具类使用需知: * * 1.计算

hdu3461Marriage Match IV 最短路+最大流

//给一个图.给定起点和终点,仅仅能走图上的最短路 //问最多有多少种走的方法.每条路仅仅能走一次 //仅仅要将在最短路上的全部边的权值改为1.求一个最大流即可 #include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<vector> using namespace std ; const int inf = 0x3f3f3f3f ; const

Java学习之IO流三

1.从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中(高效流) 1 /** 2 * 1.从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中 3 * @author vanguard 4 * 5 */ 6 public class Demo01 { 7 public static void main(String[] args) { 8 //键盘输入两个文件夹路径 9 Scanner sc = new Scanner(System.in); 1

标准文档流

标准流指的是在不使用其他的与排列和定位相关的特殊CSS规则时,各种元素的排列规则.HTML文档中的元素可以分为两大类:行内元素和块级元素.       1.行内元素不占据单独的空间,依附于块级元素,行内元素没有自己的区域.它同样是DOM树中的一个节点,在这一点上行内元素和块级元素是没有区别的.       2.块级元素总是以块的形式表现出来,并且跟同级的兄弟块依次竖直排列,左右自动伸展,直到包含它的元素的边界,在水平方向不能并排.盒子在标准流中的定位原则margin控制的是盒子与盒子之间的距离,

Properties-转换流-打印流-序列化和反序列化-Commons-IO工具类

一.Properties 类(java.util)     概述:Properties 是一个双列集合;Properties 属于map的特殊的孙子类;Properties 类没有泛型,properties集合的key和value都是固定的数据类型(String),该集合提供了一些特有的方法存取值,是唯一一个可以与IO流相结合的集合; 定义:public class Properties extends Hashtable

14. 流、文件和IO

前言 InputStream/OutStream流用来处理设备之间的数据传输 Java.io 包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io 包中的流支持很多种格式,比如:基本类型.对象.本地化字符集等等. 一个流可以理解为一个数据的序列.输入流表示从一个源读取数据,输出流表示向一个目标写数据. 流按操作数据分为两种:字节流与字符流 按流向分为:输入流(InputStream)和输出流(OutputStream) Java 为 I/O 提供了强大的而