Data Transmission

Special JudgeTime Limit: 12000/6000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)
 

Problem Description

Recently one known microprocessor productioner has developed the new type of microprocessors that can be used in difficult mathematical calculations. The processor contains N so called nodes that are connected by M channels. Data organized in packets, pass from source node to target node by channels and are processed by the intermediate nodes.

Each node has its level that determines the type of work this node does. The source node has level 1 while the target node has level L. For data to be correctly processed each packet of it must pass in order all nodes with levels from 1 to L - that is, first it must be processed by the source node, after that by some node of level 2, so on, and finally by the target node.

Nodes can process as much data as they are asked to, however channels can only transmit the limited amount of data in a unit of time. For synchronization reasons, any data can only be transmitted from a node with level i to some node with level i + 1 and cannot be transmitted between nodes which levels differ by more than one or from a node of higher level to a node of lower level. Nodes are so fast that they can process data packet immediately, so as soon as it reaches the node it is ready to be transmitted to the node of the next level.

No data should stall in any node and no node can produce its own data, so each unit of time the number of packets coming to any node except source and target, must be equal to the number of packets leaving this node.

The scheme of data transmission that satisfies the conditions provided is called the data flow. Data flow is called blocking if there is no way to increase the value of the data flow just increasing the amount of data passing by some channels (however, there may be the way to increase it, decreasing the amount of data for some channels and increasing for other ones).

Input

The first line of the input file contains three integer numbers - N, M and L (2 <= N <= 1 500, 1 <= M <= 300 000, 2 <= L <= N). Let nodes be numbered from 1 to N. The second line contains N integer numbers, i-th of them is the level li of the i-th node (1 <= li <= L). Only one node has level 1, that is the source node, and only one node has level L - that is the target node.

Next M lines describe channels, each lines contains three integer numbers a, b and c - nodes connected by this channel and its capacity in packets per unit of time (1 <= a, b <= N, lb = la+1, 1 <= c <= 106).

Two nodes can be connected by at most one channel.

Output

      Output the description of the data flow found. Output file must contain M lines, they must correspond to channels and contain the amount of data transmitted by the channel in a unit of time. Channels must be listed in the order they are specified in the input file.

Sample Input

6 7 4
1 2 3 4 3 2
1 2 3
2 3 3
3 4 4
1 6 4
6 3 2
5 4 3
6 5 4

Sample Output

3
3
4
4
1
3
3

Source

Andrew Stankevich Contest 3

Manager

 
解题:dinic算法+贪心初始流。。。太BT的一道题目。。。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,flow,next;
arc(int x = ,int y = ,int z = -){
to = x;
flow = y;
next = z;
}
};
arc e[];
int head[maxn],d[maxn],lev[maxn],_rank[maxn],in[maxn],out[maxn];
int n,m,L,S,T,hd,tl,tot,cur[maxn],q[maxn];
void myscanf(int &x){
char ch;
while((ch = getchar()) > '' || ch < '');
x = ;
x = x* + ch - '';
while((ch = getchar()) >= '' && ch <= '')
x = x* + ch - '';
}
void add(int u,int v,int flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
bool cmp(const int &x,const int &y){
return lev[x] < lev[y];
}
void greedy(){
memset(in,,sizeof(in));
memset(out,,sizeof(out));
sort(_rank+,_rank+n+,cmp);
in[S] = INF;
for(int i = ; i <= n; i++){
int u = _rank[i];
for(int j = head[u]; ~j; j = e[j].next){
if(!(j&) && in[u] > out[u]){
int f = min(e[j].flow,in[u] - out[u]);
in[e[j].to] += f;
out[u] += f;
}
}
}
memset(in,,sizeof(in));
in[T] = INF;
for(int i = n; i >= ; --i){
int v = _rank[i];
for(int j = head[v]; ~j; j = e[j].next){
int u = e[j].to;
if(j& && out[u] > in[u]){
int f = min(e[j^].flow,min(out[u] - in[u],in[v]));
in[v] -= f;
in[u] += f;
e[j].flow += f;
e[j^].flow -= f;
}
}
}
}
bool bfs(){
memset(d,-,sizeof(d));
hd = tl = ;
q[tl++] = S;
d[S] = ;
while(hd < tl){
int u = q[hd++];
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q[tl++] = e[i].to;
}
}
}
return d[T] > -;
}
int dfs(int u,int low){
if(u == T) return low;
int tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] == d[u] + && (a=dfs(e[i].to,min(low,e[i].flow)))){
tmp += a;
low -= a;
e[i].flow -= a;
e[i^].flow += a;
if(!low) break;
}
}
if(!tmp) d[u] = -;
return tmp;
}
int dinic(){
int tmp = ;
while(bfs()){
memcpy(cur,head,sizeof(head));
tmp += dfs(S,INF);
}
return tmp;
}
int main() {
int i,u,v,cap;
scanf("%d %d %d",&n,&m,&L);
memset(head,-,sizeof(head));
for(i = ; i <= n; i++){
myscanf(lev[i]);
_rank[i] = i;
if(lev[i] == ) S = i;
else if(lev[i] == L) T = i;
}
for(int i = tot = ; i < m; i++){
myscanf(u);
myscanf(v);
myscanf(cap);
add(u,v,cap);
}
greedy();
dinic();
for(int i = ; i < m; i++)
printf("%d\n",e[i<<|].flow);
return ;
}

ACdream 1229 Data Transmission的更多相关文章

  1. ZOJ-2364 Data Transmission 分层图阻塞流 Dinic+贪心预流

    题意:给定一个分层图,即只能够在相邻层次之间流动,给定了各个顶点的层次.要求输出一个阻塞流. 分析:该题直接Dinic求最大流TLE了,网上说采用Isap也TLE,而最大流中的最高标号预流推进(HLP ...

  2. Toward Scalable Systems for Big Data Analytics: A Technology Tutorial (I - III)

    ABSTRACT Recent technological advancement have led to a deluge of data from distinctive domains (e.g ...

  3. Chrysler -- CCD (Chrysler Collision Detection) Data Bus

    http://articles.mopar1973man.com/general-cummins/34-engine-system/81-ccd-data-bus CCD (Chrysler Coll ...

  4. Efficient data transfer through zero copy

    Efficient data transfer through zero copy https://www.ibm.com/developerworks/library/j-zerocopy/ Eff ...

  5. Buffer Data

    waylau/netty-4-user-guide: Chinese translation of Netty 4.x User Guide. 中文翻译<Netty 4.x 用户指南> h ...

  6. Data Replication in a Multi-Cloud Environment using Hadoop & Peer-to-Peer technologies

    http://fbevmware.blogspot.com/2013/12/data-replication-in-multi-cloud.html 要FQ... —————————————————— ...

  7. PatentTips – RDMA data transfer in a virtual environment

    BACKGROUND Embodiments of this invention relate to RDMA (remote direct memory access) data transfer ...

  8. Indexing Sensor Data

    In particular embodiments, a method includes, from an indexer in a sensor network, accessing a set o ...

  9. Data analysis system

    A data analysis system, particularly, a system capable of efficiently analyzing big data is provided ...

随机推荐

  1. xocde8打印出:Presenting view controllers on detached view controllers is discouraged

    原因: 是某个viewController的生命周期控制出现了错误,所以尽量避免一个controller的view去addsubview另一个controller的view,这样会破坏层级关系,导致第 ...

  2. hdoj--1281--棋盘游戏(最小点覆盖+枚举)

    棋盘游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  3. [ASPX] 模版引擎XTemplate与代码生成器XCoder(源码)

    模版引擎XTemplate是一个仿T4设计的引擎,功能上基本与T4一致(模版语法上完全兼容T4,模版头指令部分兼容). 自己设计模版引擎,就是为了代码生成器.网站模版.邮件模版等多种场合,也就是要能拿 ...

  4. SpringBoot中拦截器和过滤器的使用

    一.拦截器 三种方式 继承WebMvcConfigurerAdapter   spring5.0 以弃用,不推荐 实现WebMvcConfigurer  推荐 继承WebMvcConfiguratio ...

  5. B - Calculating Function

    Problem description For a positive integer n let's define a function f: f(n) =  - 1 + 2 - 3 + .. + ( ...

  6. html+css布局整理笔记

    基本概念 布局模型 流动模型(Flow) 浮动模型(Float) 层模型(Layer) 流动模型 默认的网页布局模式,流动布局模型有两个比较典型的特征: 第一,块级元素都会在所处的包含元素内自上而下按 ...

  7. 无序列表属性 隐藏方式 JS简介

    今天考试了,整理一下错题. 1.无序列表的属性 list-style 分为三小类 (1)list-style-type none:无标记. disc:实心圆(默认). circle:空心圆. squa ...

  8. BZOJ3573: [Hnoi2014]米特运输(树上乱搞)

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1669  Solved: 1031[Submit][Status][Discuss] Descript ...

  9. 用opcity模拟zindex渐变的效果

    github地址: https://github.com/echoorx/opacity-Gradient zindex好像不能渐变改变,所以用opcity来模拟 <!DOCTYPE html& ...

  10. 第五课: - Stack / Unstack / Transpose函数

    第 5 课   我们将简要介绍 stack 和 unstack 以及 T (Transpose)函数. 在用pandas进行数据重排时,经常用到stack和unstack两个函数.stack的意思是堆 ...