Reverse a Road II
Time Limit: 10000ms, Special Time Limit:25000ms, Memory Limit:65536KB
Total submit users: 10, Accepted users: 6
Problem 13411 : No special judgement
Problem description

JAG Kingdom is a strange kingdom such that its N cities are connected only by one-way roads. The N cities are numbered 1 through N. ICPC (International Characteristic Product Corporation) transports its products from the factory at the city S to the storehouse
at the city T in JAG Kingdom every day. For efficiency, ICPC uses multiple trucks at once. Each truck starts from S and reaches T on the one-way road network, passing through some cities (or directly). In order to reduce risks of traffic jams and accidents,
no pair of trucks takes the same road.



Now, ICPC wants to improve the efficiency of daily transports, while ICPC operates daily transports by as many trucks as possible under the above constraint. JAG Kingdom, whose finances are massively affected by ICPC, considers to change the direction of one-way
roads in order to increase the number of trucks for daily transports of ICPC. Because reversal of many roads causes confusion, JAG Kingdom decides to reverse at most a single road.



If there is no road such that reversal of the road can improve the transport efficiency, JAG Kingdom need not reverse any roads. Check whether reversal of a single road can improve the current maximum number of trucks for daily transports. And if so, calculate
the maximum number of trucks which take disjoint sets of roads when a one-way road can be reversed, and the number of roads which can be chosen as the road to be reversed to realize the maximum.

Input

The input consists of multiple datasets. The number of dataset is no more than 100.



Each dataset is formatted as follows.



 

The following M lines describe the information of the roads. The i-th line of them contains two integers aiand bi(1 ≤ ai,bi≤ N, ai≠ bi), meaning that the i-th road is directed from ai to bi.



The end of input is indicated by a line containing four zeros.

Output

For each dataset, output two integers separated by a single space in a line as follows: If reversal of a single road improves the current maximum number of trucks for daily transports, the first output integer is the new maximum after reversal of a road,
and the second output integer is the number of roads which can be chosen as the road to be reversed to realize the new maximum. Otherwise, i.e. if the current maximum cannot be increased by any reversal of a road, the first output integer is the current maximum
and the second output integer is 0.

Sample Input
4 4 1 4
1 2
3 1
4 2
3 4
7 8 1 7
1 2
1 3
2 4
3 4
4 5
4 6
5 7
7 6
6 4 5 2
1 2
1 3
4 5
5 6
10 21 9 10
9 1
9 2
9 3
9 4
10 1
10 2
10 3
10 4
1 5
2 5
2 6
3 6
3 7
4 7
4 8
1 8
5 10
6 10
7 10
10 8
10 9
2 15 1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
0 0 0 0
Sample Output
1 2
2 1
0 0
4 6
15 0
Problem Source

JAG Practice Contest for ACM-ICPC Asia Regional 2014

题意:给一个有向图,n个点,m条边,起点S ,终点T。现有非常多个机器人要从S走到T,每条边仅仅能走一个机器人,现要能够改一条边的方向,问最多有多少个机器人能够从S走到T。而且有多少种改法。(假设没有改变边的方向,则改法为0。)

解题:如要没有加条件:能够改一条边。那么这题就是找最多有多少条路要以从起点S走到T。建图:每条有向边的边权为1,用最大流跑一次。那么如今要改一条边的方向:假设要更改的边是走过的边,那么不会使走法添加,有能够还使路的条数降低,所以改的边一定不是满流的边(即走过的边)。如今要改变一条不在走过的边< u  ,  v >变成<v , u>,使得能够增流,那么仅仅要从S沿着没有走过的边到  v 。再从u沿着没有走过的边 到 T。这样就能够使得路的条数增多。

那么这样我们就能够用BFS标记 SF[] 能从S点沿着最大流每条边的残流量>0的边走到的一些点V,再标记
TF[] 能从T点沿着最大流每条边的残流量==0的边走到一些点U(即表示:能从U沿着那些边走到T)。最后仅仅要枚举原来不在走过的边<u , v >,假设:SF[v]==1&&TF[u]==1则改变此边方向能够增流。方案数k+1。

最后的最多机器人数ans = maxflow + k>0 ? 1 : 0  。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define captype int const int MAXN = 1010; //点的总数
const int MAXM = 40010; //边的总数
const int INF = 1<<30;
struct EDG{
int to,next;
captype cap;
} edg[MAXM];
int eid,head[MAXN];
int gap[MAXN]; //每种距离(或可觉得是高度)点的个数
int dis[MAXN]; //每一个点到终点eNode 的最短距离
int cur[MAXN]; //cur[u] 表示从u点出发可流经 cur[u] 号边
int pre[MAXN]; void init(){
eid=0;
memset(head,-1,sizeof(head));
}
//有向边 三个參数。无向边4个參数
void addEdg(int u,int v,captype c,captype rc=0){
edg[eid].to=v; edg[eid].next=head[u];
edg[eid].cap=c; head[u]=eid++; edg[eid].to=u; edg[eid].next=head[v];
edg[eid].cap=rc; head[v]=eid++;
}
captype maxFlow_sap(int sNode,int eNode, int n){//n是包含源点和汇点的总点个数,这个一定要注意 memset(gap,0,sizeof(gap));
memset(dis,0,sizeof(dis));
memcpy(cur,head,sizeof(head));
pre[sNode] = -1;
gap[0]=n;
captype ans=0; //最大流
int u=sNode;
while(dis[sNode]<n){ //推断从sNode点有没有流向下一个相邻的点
if(u==eNode){ //找到一条可增流的路 for(int i=pre[u]; i!=-1; i=pre[edg[i^1].to]){
edg[i].cap-=1;
edg[i^1].cap+=1; //可回流的边的流量
}
ans+=1;
u=sNode;
continue;
}
bool flag = false; //推断是否能从u点出发可往相邻点流
int v;
for(int i=cur[u]; i!=-1; i=edg[i].next){
v=edg[i].to;
if(edg[i].cap>0 && dis[u]==dis[v]+1){
flag=true;
cur[u]=pre[v]=i;
break;
}
}
if(flag){
u=v;
continue;
}
//假设上面没有找到一个可流的相邻点。则改变出发点u的距离(也可觉得是高度)为相邻可流点的最小距离+1
int Mind= n;
for(int i=head[u]; i!=-1; i=edg[i].next)
if(edg[i].cap>0 && Mind>dis[edg[i].to]){
Mind=dis[edg[i].to];
cur[u]=i;
}
gap[dis[u]]--;
if(gap[dis[u]]==0) return ans; //当dis[u]这样的距离的点没有了,也就不可能从源点出发找到一条增广流路径
//由于汇点到当前点的距离仅仅有一种,那么从源点到汇点必定经过当前点。然而当前点又没能找到可流向的点,那么必定断流
dis[u]=Mind+1;//假设找到一个可流的相邻点。则距离为相邻点距离+1,假设找不到,则为n+1
gap[dis[u]]++;
if(u!=sNode) u=edg[pre[u]^1].to; //退一条边
}
return ans;
}
bool flag[MAXM] , SF[MAXN] , TF[MAXN];
void bfs(int s ,bool flag)
{
queue<int>q;
int u , v;
q.push(s );
while(!q.empty())
{
u = q.front(); q.pop();
for(int i=head[u]; ~i ; i=edg[i].next)
{
v=edg[i].to;
if(flag)
{
if(SF[v]||!edg[i].cap)continue;
SF[v]=1;
q.push(v);
}
else{
if(TF[v]||!edg[i^1].cap)continue;
TF[v]=1;
q.push(v);
}
}
}
}
inline void scanf(int& num )
{
char ch;
while(ch=getchar())
{
if(ch>='0'&&ch<='9')break;
}
num = ch-'0';
while(ch=getchar())
{
if(ch<'0'||ch>'9')break;
num = num*10+ch-'0';
}
}
int main()
{
int n,m,vs , vt , u ,v; while(scanf("%d%d%d%d",&n,&m,&vs,&vt)>0&&n+m+vs+vt!=0)
{
init();
for(int i=0; i<m; i++)
{
scanf(u);
scanf(v);
addEdg(u , v ,1);
}
memset(flag , 0 , (m+3)*sizeof(bool));
memset(SF,0,sizeof(SF));
memset(TF,0,sizeof(TF));
int ans , k=0 ;
ans = maxFlow_sap(vs , vt , n );
for(int i=0; i<m; i++)
if(edg[i<<1].cap==0)
flag[i]=1;
SF[vs ] =1; bfs(vs , 1);
TF[vt] = 1; bfs(vt , 0); for(int i=0; i<m; i++)
if(!flag[i]){
v=edg[i<<1|1].to;
u=edg[i<<1].to; if(SF[u]&&TF[v])
k++;
}
if(k) ans++;
printf("%d %d\n",ans , k);
}
}

HNU 13411 Reverse a Road II(最大流+BFS)经典的更多相关文章

  1. 【leetcode】Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

  2. 14. Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

  3. 【原创】Leetcode -- Reverse Linked List II -- 代码随笔(备忘)

    题目:Reverse Linked List II 题意:Reverse a linked list from position m to n. Do it in-place and in one-p ...

  4. 92. Reverse Linked List II

    题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1- ...

  5. lc面试准备:Reverse Linked List II

    1 题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1 ...

  6. 【LeetCode练习题】Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

  7. [Linked List]Reverse Linked List,Reverse Linked List II

    一.Reverse Linked List  (M) Reverse Linked List II (M) Binary Tree Upside Down (E) Palindrome Linked ...

  8. LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

    1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...

  9. 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 线段树维护dp

    题目 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 链接 http://www.lydsy.com/JudgeOnline/proble ...

随机推荐

  1. 【单调队列】【动态规划】bzoj3831 [Poi2014]Little Bird

    f(i)=min{f(j)+(D(j)<=D(i))} (max(1,i-k)<=j<=i) 有两个变量,很难用单调队列,但是(引用): 如果fi<fj,i一定比j优秀.因为如 ...

  2. [CF877F]Ann and Books

    题目大意: 有$n(n\le10^5)$个数$w_{1\sim n}(|w_i|\le10^9)$,并给定一个数$k(|k|\le10^9)$.$q(q\le10^5)$次询问,每次询问区间$[l,r ...

  3. NPOI读取Excel2003,2007

    using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Lin ...

  4. Scala高手实战****第18课:Scala偏函数、异常、Lazy值编码实战及Spark源码鉴赏

    本篇文章主要讲述Scala函数式编程之偏函数,异常,及Lazy 第一部分:偏函数 偏函数:当函数有多个参数,而在使用该函数时不想提供所有参数(比如函数有3个参数),只提供0~2个参数,此时得到的函数便 ...

  5. C# 6.0语法新特性体验(二)

    之前我在文章通过Roslyn体验C# 6.0的新语法中介绍了一些C# 6.0的语法特性,现在随着Visual Studio 14 CTP3的发布,又陆续可以体验一些新的特性了,这里简单的介绍一下之前没 ...

  6. 《西安交大电路》(Principles of Electrical Circuits) 学习笔记

    内容简介:电路分析是电子类专业的第一门基础课. 电路理论包括电路分析和电路综合两大方面内容.电路分析的主要内容是指在给定电路结构.元件参数的条件下,求取由输入(激励)所产生的输出(响应):电路综合则主 ...

  7. 【java】子类可以通过调用父类的public方法调用父类的private方法,为什么?

    代码1: 打印结果: 代码2: 运行结果: 问题: 代码1中super是父类自己调用自己的add()方法,并在add()方法中调用了私有的del()方法,那为什么打印出来的this是子类? 代码2中t ...

  8. jquery获取下拉列表的值和显示内容的方法

    页面的下拉列表: 选择时间段: <select name="timespan" id="timespan" class="Wdate" ...

  9. ylbtech-LanguageSamples-Arrays(数组)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Arrays(数组) 1.A,示例(Sample) 返回顶部 “数组”示例 本示例介绍并 ...

  10. ubuntu查看系统版本

    1.查看文件信息,包含32-bit就是32位,包含64-bit就是64位 root@HDController:/home/nulige/tools# uname -a Linux HDControll ...