UVALive 2957 Bring Them There
Bring Them There
This problem will be judged on UVALive. Original ID: 2957
64-bit integer IO format: %lld Java class name: Main
By the year 3141, the human civilization has spread all over the galaxy. The special hypertunnels are used to travel from one star system to another. To use the hypertunnel, you fly to a special location near the source star using your spaceship, activate the hyperjumper, fly through the hypertunnel, get out near your destination star and fly to the planet you need. The whole process takes exactly one day. A small drawback of the system is that for each tunnel every day only one spaceship can travel using this tunnel.
You are working in the transportation department of the ``Intergalaxy Business Machines" company. This morning your boss has assigned a new task to you. To run the programming contest IBM needs to deliver K supercomputers from Earth where the company headquarters are located to the planet Eisiem. Since supercomputers are very large, one needs the whole spaceship to carry each supercomputer. You are asked to find a plan to deliver the supercomputers that takes as few days as possible. Since IBM is a very powerful corporation, you may assume that any time you need some tunnel for hyperjump, it is at your service. However, you still can use each tunnel only once a day.
Input
Input consists of several datasets. The first line of each dataset contains N - the number of star systems in the galaxy, M - the number of tunnels, K - the number of supercomputers to be delivered, S - the number of the solar system (the system where planet Earth is) and T - the number of the star system where planet Eisiem is (2
N
50, 1
M
200, 1
K
50, 1
S, T
N, S
T).
Next M lines contain two different integer numbers each and describe tunnels. For each tunnel the numbers of star systems that it connects are given. The tunnel can be traveled in both directions, but remember that each day only one ship can travel through it, in particular, two ships cannot simultaneously travel through the same tunnel in opposite directions. No tunnel connects a star to itself and any two stars are connected by at most one tunnel.
Output
On the first line of the output for each dataset print L - the fewest number of days needed to deliver K supercomputers from star system S to star system T using hypertunnels. Next Llines must describe the process. Each line must start with Ci - the number of ships that travel from one system to another this day. Ci pairs of integer numbers must follow, pair A, Bmeans that the ship number A travels from its current star system to star system B.
It is guaranteed that there is a way to travel from star system S to star system T.
Sample Input
6 7 4 1 6
1 2
2 3
3 5
5 6
1 4
4 6
4 3
Sample Output
4
2 1 2 2 4
3 1 3 2 6 3 4
3 1 5 3 6 4 4
2 1 6 4 6
Source
#include <bits/stdc++.h>
using namespace std;
const int INF = ~0U>>;
const int maxn = ;
struct arc {
int to,flow,next;
arc(int x = ,int y = ,int z = -) {
to = x;
flow = y;
next = z;
}
} e[maxn*];
int head[maxn],d[maxn],cur[maxn],tot,S,T;
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 bfs() {
queue<int>q;
memset(d,-,sizeof d);
q.push(S);
d[S] = ;
while(!q.empty()) {
int u = q.front();
q.pop();
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.push(e[i].to);
}
}
}
return d[T] > -;
}
int dfs(int u,int low) {
if(u == T) return low;
int a,tmp = ;
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)))) {
e[i].flow -= a;
e[i^].flow += a;
low -= a;
tmp += a;
break;
}
}
if(!tmp) d[u] = -;
return tmp;
}
int dinic(int bound,int ret = ) {
while(ret < bound && bfs()) {
memcpy(cur,head,sizeof head);
ret += dfs(S,INF);
}
return ret;
}
int n,m,k,s,t;
int x[maxn],y[maxn];
void output(int day) {
int to[],vis[],s[],t[];
for(int i = ; i <= k; ++i) to[i] = S;
int id = ;
for(int d = ; d <= day; ++d) {
id += (n<<); //跳过计算机停留在某个星球上一天的边
int cnt = ;
for(int i = ; i < m; ++i) {
int flow1 = e[id].flow;
id += ;
int flow2 = e[id].flow;
id += ;
if(flow1 && !flow2) s[cnt] = y[i], t[cnt++] = x[i];
if(flow2 && !flow1) s[cnt] = x[i], t[cnt++] = y[i];
}
memset(vis,,sizeof vis);
printf("%d", cnt);
for(int i = ; i < cnt; ++i)
for(int j = ; j <= k; ++j)
if(s[i] == to[j] && !vis[j]) {
printf(" %d %d", j, t[i]);
to[j] = t[i];
vis[j] = ;
break;
}
printf("\n");
}
}
void solve() {
int ret = ,day = ;
while(ret < k) {
++day;
for(int i = ; i <= n; ++i)
add((day - )*n + i,day*n + i,INF);
for(int i = ; i < m; ++i) {
add(x[i] + (day - )*n,y[i] + day*n,);
add(y[i] + (day - )*n,x[i] + day*n,);
}
S = s;
T = t + day*n;
ret += dinic(k - ret);
}
printf("%d\n",day);
output(day);
}
int main() {
while(~scanf("%d%d%d%d%d",&n,&m,&k,&s,&t)) {
memset(head,-,sizeof head);
for(int i = tot = ; i < m; ++i)
scanf("%d%d",x + i,y + i);
solve();
}
return ;
}
UVALive 2957 Bring Them There的更多相关文章
- 【清华集训】楼房重建 BZOJ 2957
Description 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子. 为了简化问题,我们考虑这些 ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- HYSBZ 2957 分块
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2957 题意:中文题面 思路: 来自此博客 首先明确问题,对于每栋楼房的斜率K=H/X,问题 ...
- [BZOJ 2957]楼房重建(THU2013集训)(分块思想)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2957 分析: 首先明确问题,对于每栋楼房的斜率K=H/X,问题就是问有多少个楼房的K比前面所有 ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- he time that it takes to bring a block from disk into main memory
DATABASE SYSTEM CONCEPTS, SIXTH EDITION There is a trade-off that the system designer must make betw ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
随机推荐
- Hibernate Could not obtain transaction-synchronized Session for current thread问题处理
项目通过Hibernate查询时报出如下错误: Hibernate Could not obtain transaction-synchronized Session for current thre ...
- 接口测试01 - HTTP协议报文结构及示例
HTTP基本架构 用一张简单的流程图来展示HTTP协议的基本架构,以便先有个基础的了解. 1)Web Client可以是浏览器.搜索引擎等等一切基于HTTP协议发起http请求的工具. 2)Web S ...
- ping localhost出现地址::1
近期发现本机的localhost不好用了. 症状: 自己本机部署服务器时,浏览器地址栏访问localhost:8080不通: 禁用网络连接,未果: 拔出网线,OK. cmd里ping之,返回结果如下: ...
- Properties没有被注意的地方
源起: 今天阅读源码时发现一个地方不理解: 为什么以下代码第10行 get() 之后value为null时还去 getProperty() 呢? org.springframework.util.Co ...
- P2142 高精度减法
题目描述 高精度减法 输入输出格式 输入格式: 两个整数a,b(第二个可能比第一个大) 输出格式: 结果(是负数要输出负号) 输入输出样例 输入样例#1: 2 1 输出样例#1: 1 说明 20%数据 ...
- 【学习笔记】using namespace std 的作用
C++编程时几乎每次都敲上using namespace std;但这行代码究竟有什么作用呢? C++标准程序库中的所有标识符都被定义于一个名为std的namespace中. 早些的编码将标准库功能定 ...
- iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
- CSV解析
NSString *path = [[NSBundle mainBundle] pathForResource: @"type" ofType:@"csv"]; ...
- java语言基础-类型运算细节
代码一: public class varDemo{ public static void main(String[] args) { byte a2; a2=3+4; System.out.prin ...
- PHP一句话后门过狗姿势万千之理论篇
写在前面: 过狗相关的资料网上也是有很多,所以在我接下来的文章中,可能观点或者举例可能会与网上部分雷同,或者表述不够全面. 但是我只能说,我所传达给大家的信息,是我目前所掌握或者了解的,不能保证所有人 ...