FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes them shake in their hooves. They have decided to put a rain siren on the farm to let them know when rain is approaching. They intend to create a rain evacuation plan so that all the cows can get to shelter before the rain begins. Weather forecasting is not always correct, though. In order to minimize false alarms, they want to sound the siren as late as possible while still giving enough time for all the cows to get to some shelter.
The farm has F (1 <= F <= 200) fields on which the cows graze. A set of P (1 <= P <= 1500) paths connects them. The paths are wide, so that any number of cows can traverse a path in either direction.
Some of the farm's fields have rain shelters under which the cows can shield themselves. These shelters are of limited size, so a single shelter might not be able to hold all the cows. Fields are small compared to the paths and require no time for cows to traverse.
Compute the minimum amount of time before rain starts that the siren must be sounded so that every cow can get to some shelter.
Input
* Line 1: Two space-separated integers: F and P
* Lines 2..F+1: Two space-separated integers that describe a field. The first integer (range: 0..1000) is the number of cows in that field. The second integer (range: 0..1000) is the number of cows the shelter in that field can hold. Line i+1 describes field i.
* Lines F+2..F+P+1: Three space-separated integers that describe a path. The first and second integers (both range 1..F) tell the fields connected by the path. The third integer (range: 1..1,000,000,000) is how long any cow takes to traverse it.
Output
* Line 1: The minimum amount of time required for all cows to get under a shelter, presuming they plan their routes optimally. If it not possible for the all the cows to get under a shelter, output "-1".
Sample Input
3 4 7 2 0 4 2 6 1 2 40 3 2 70 2 3 90 1 3 120
Sample Output
110
思路:因为要求出任意两个点之间的最短路,所以要Flody,这里网路流建边的时候给每条边都设一个长度,方便之后二分, 然后把每个篷拆成一个前驱一个后继,s连所有前驱,容量为初始牛棚牛的个数,长度为0, 后继连t,容量为牛棚可容纳容量,长度为0,然后每个点的前驱想后继建边,容量为INF, 长度为两点之间的最短距离。跑DInic,若最大流等于牛的总数,则说明当前时间可行。由于最长距离可能会达到1e9 x 200,所以要开long long ,(这里因为我太懒,就所有变量都开long long了
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
typedef long long LL;
LL INF = 1e18 + 10;
const LL maxn = 1e5 + 50;
struct Edge
{
LL to, next, w, co;
} edge[maxn];
LL k, head[maxn];
void add(LL a, LL b, LL c, LL co){
edge[k].co = co;
edge[k].to = b;
edge[k].w = c;
edge[k].next = head[a];
head[a] = k++;
edge[k].co = co;
edge[k].to = a;
edge[k].w = 0;
edge[k].next = head[b];
head[b] = k++;
}
LL dis[maxn], cur[maxn];
LL mid;
LL bfs(LL s, LL t){
for(LL i = 0; i <= t; i++){
dis[i] = 0;
}
dis[s] = 1;
queue<LL> que;
que.push(s);
while(que.size()){
LL u = que.front();
que.pop();
for(LL i = head[u]; i != -1; i = edge[i].next){
if(edge[i].co > mid){
continue;
}
LL to = edge[i].to;
LL w = edge[i].w;
if(w && !dis[to]){
dis[to] = dis[u] + 1;
if(to == t){
return 1;
}
que.push(to);
}
}
}
return 0;
}
LL dfs(LL u, LL maxf, LL t){
if(u == t){
return maxf;
}
LL ret = 0;
for(LL i = cur[u]; i != -1; i = edge[i].next){
if(edge[i].co > mid){
continue;
}
cur[u] = i;
LL to = edge[i].to;
LL w = edge[i].w;
if(dis[to] == dis[u] + 1 && w){
LL mi = min(maxf - ret, w);
w = dfs(to, mi, t);
edge[i].w -= w;
edge[i ^ 1].w += w;
ret += w;
if(ret == maxf){
return ret;
}
}
}
return ret;
}
LL Dinic(LL s, LL t){
LL ans = 0;
while(bfs(s, t)){
for(LL i = 0; i <= t; i++){
cur[i] = head[i];
}
ans += dfs(s, INF, t);
}
return ans;
}
LL mp[255][255];
LL ss[maxn], tt[maxn];
int main(int argc, char const *argv[])
{
LL n, m;
scanf("%lld%lld", &n, &m);
LL sum = 0;
for(LL i = 1; i <= n; i++){
scanf("%lld%lld", &ss[i], &tt[i]);
sum += ss[i];
}
for(LL i = 1; i <= n; i++){
for(LL j = 1; j <= n; j++){
if(i != j){
mp[i][j] = INF;
}
}
}
for(LL i = 0; i < m; i++){
LL a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
mp[a][b] = mp[b][a] = min(mp[a][b], c);
}
for(LL k = 1; k <= n; k++){
for(LL i = 1; i <= n; i++){
for(LL j = 1; j <= n; j++){
mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);
}
}
}
LL s = 0, t = n * 2 + 1;
LL le = 1, ri = 1e12;
LL ans = INF;
while(le <= ri){
k = 0;
mid = (le + ri) >> 1;
for(LL i = 0; i <= t; i++){
head[i] = -1;
}
for(LL i = 1; i <= n; i++){
add(s, i, ss[i], 0);
add(i + n, t, tt[i], 0);
}
for(LL i = 1; i <= n; i++){
for(LL j = 1; j <= n; j++){
if(mp[i][j] < INF){
add(i, j + n, INF, mp[i][j]);
}
}
}
LL res = Dinic(s, t);
if(res == sum){
ri = mid - 1;
ans = min(ans, mid);
} else{
le = mid + 1;
}
}
if(ans == INF){
printf("-1\n");
} else{
printf("%lld\n", ans);
}
return 0;
}