CF843E.Maximum Flow

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a directed graph, consisting of nn vertices and mm edges. The vertices ss and tt are marked as source and sink correspondingly. Additionally, there are no edges ending at ss and there are no edges beginning in tt .

The graph was constructed in a following way: initially each edge had capacity c_{i}>0 . A maximum flow with source at ss and sink at tt was constructed in this flow network. Let's denote fif_{i} as the value of flow passing through edge with index ii . Next, all capacities cic_{i} and flow value fif_{i} were erased. Instead, indicators gig_{i} were written on edges — if flow value passing through edge ii was positive, i.e. 11 if f_{i}>0 and 00 otherwise.

Using the graph and values gig_{i} , find out what is the minimum possible number of edges in the initial flow network that could be saturated (the passing flow is equal to capacity, i.e. fi=cif_{i}=c_{i} ). Also construct the corresponding flow network with maximum flow in it.

A flow in directed graph is described by flow values fif_{i} on each of the edges so that the following conditions are satisfied:

  • for each vertex, except source and sink, total incoming flow and total outcoming flow are equal,
  • for each edge 0<=fi<=ci0<=f_{i}<=c_{i}

A flow is maximum if the difference between the sum of flow values on edges from the source, and the sum of flow values on edges to the source (there are no such in this problem), is maximum possible.

输入格式

The first line of input data contains four positive integers n,m,s,tn,m,s,t ( 2<=n<=1002<=n<=100 , 1<=m<=10001<=m<=1000 , 1<=s,t<=n1<=s,t<=n , sts≠t ) — the number of vertices, the number of edges, index of source vertex and index of sink vertex correspondingly.

Each of next mm lines of input data contain non-negative integers uiu_{i} , viv_{i} , gig_{i} ( 1<=ui,vi<=n1<=u_{i},v_{i}<=n , ) — the beginning of edge ii , the end of edge ii and indicator, which equals to 11 if flow value passing through edge ii was positive and 00 if not.

It's guaranteed that no edge connects vertex with itself. Also it's guaranteed that there are no more than one edge between each ordered pair of vertices and that there exists at least one network flow that satisfies all the constrains from input data.

输出格式

In the first line print single non-negative integer kk — minimum number of edges, which should be saturated in maximum flow.

In each of next mm lines print two integers fi,cif_{i},c_{i} ( 1<=ci<=1091<=c_{i}<=10^{9} , 0<=fi<=ci0<=f_{i}<=c_{i} ) — the flow value passing through edge ii and capacity of edge ii .

This data should form a correct maximum flow in flow network. Also there must be exactly kk edges with statement fi=cif_{i}=c_{i} satisfied. Also statement f_{i}>0 must be true if and only if gi=1g_{i}=1 .

If there are several possible answers, print any of them.

输入输出样例

  • 输入#1

    5 6 1 5
    1 2 1
    2 3 1
    3 5 1
    1 4 1
    4 3 0
    4 5 1
    

    输出#1

    2
    3 3
    3 8
    3 4
    4 4
    0 5
    4 9
    

说明/提示

The illustration for second sample case. The saturated edges are marked dark, while edges with gi=0g_{i}=0 are marked with dotted line. The integer on edge is the index of this edge in input list.

首页