[JSOI2008]Blue Mary的旅行
嘟嘟嘟
看\(n\)那么小,就知道是网络流。然后二分,按时间拆点。
刚开始我看成所有航班一天只能起飞一次,纠结了好一会儿。但实际上是每一个航班单独考虑,互不影响。
建图很显然,拆完点后每一个点的第\(i\)天向和他相连的点的第\(i + 1\)天连边,同时自己的第\(i\)天也要向第\(i + 1\)天连边。
刚开始数组开小了,自己造了几组大数据才对。不过说来也奇怪,自己的随机数据跑出来都3、4秒,交上去却秒A了。可能是因为自己的答案都是1或2吧,出题人为了防止固输骗分,就没有造这种极端数据。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 62505;
const int maxe = 1e6 + 5;
const int maxm = 2455;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, m, tot, t;
struct Node
{
int x, y, w;
}ed[maxm];
struct Edge
{
int nxt, to, cap, flow;
}e[maxe];
int head[maxn], ecnt = -1;
In void addEdge(int x, int y, int w)
{
//printf("()%d %d %d\n", x, y, w);
e[++ecnt] = (Edge){head[x], y, w, 0};
head[x] = ecnt;
e[++ecnt] = (Edge){head[y], x, 0, 0};
head[y] = ecnt;
}
int dis[maxn];
In bool bfs()
{
Mem(dis, 0); dis[0] = 1;
queue<int> q; q.push(0);
while(!q.empty())
{
int now = q.front(); q.pop();
//printf("#%d\n", now);
for(int i = head[now], v; ~i; i = e[i].nxt)
{
if(!dis[v = e[i].to] && e[i].cap > e[i].flow)
{
dis[v] = dis[now] + 1;
q.push(v);
}
}
}
return dis[t];
}
int cur[maxn];
In int dfs(int now, int res)
{
if(now == t || res == 0) return res;
int flow = 0, f;
for(int& i = cur[now], v; ~i; i = e[i].nxt)
{
if(dis[v = e[i].to] == dis[now] + 1 && (f = dfs(v, min(res, e[i].cap - e[i].flow))) > 0)
{
e[i].flow += f, e[i ^ 1].flow -= f;
flow += f, res -= f;
if(res == 0) break;
}
}
return flow;
}
In int maxflow()
{
int flow = 0;
while(bfs())
{
memcpy(cur, head, sizeof(head));
flow += dfs(0, INF);
}
return flow;
}
int _Max = 0;
In int num(int x, int tim, int id) {return (x - 1) * tim + id;}
In bool judge(int tim)
{
ecnt = -1; Mem(head, -1);
t = num(n, tim, tim) + 1;
_Max = max(_Max, t);
for(int i = 1; i <= m; ++i)
for(int j = 1; j < tim; ++j) addEdge(num(ed[i].x, tim, j), num(ed[i].y, tim, j + 1), ed[i].w);
for(int i = 1; i <= tim; ++i)
{
addEdge(0, num(1, tim, i), INF);
addEdge(num(n, tim, i), t, INF);
}
for(int i = 1; i <= n; ++i)
for(int j = 2; j <= tim; ++j) addEdge(num(i, tim, j - 1), num(i, tim, j), INF);
return maxflow() >= tot;
}
int main()
{
//freopen("ha.in", "r", stdin);
Mem(head, -1);
n = read(), m = read(), tot = read();
for(int i = 1; i <= m; ++i) ed[i].x = read(), ed[i].y = read(), ed[i].w = read();
int L = 0, R = tot * n + 1;
while(L < R)
{
int mid = (L + R) >> 1;
if(judge(mid)) R = mid;
else L = mid + 1;
}
write(L - 1), enter;
//write(_Max), enter;
return 0;
}