Ombrophobic Bovines

Description

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

Hint

OUTPUT DETAILS:

In 110 time units, two cows from field 1 can get under the shelter in that field, four cows from field 1 can get under the shelter in field 2, and one cow can get to field 3 and join the cows from that field under the shelter in field 3. Although there are other plans that will get all the cows under a shelter, none will do it in fewer than 110 time units.
题意:给出一些牛棚,开始牛棚边有一些牛,牛棚之间有路相连,走一条路会花费固定的时间。牛在牛棚边吃草,下雨时牛得躲进牛棚,每个牛棚容量有限。
求:在所有牛都能进牛棚时最少需要多少时间。
分析:如果知道了时间time,则如果两个牛棚之间所需的最少时间<=time,每个牛棚拆成两点x1和x2。如果两个牛棚a和b之间可以相连,则ax1连ax2,容量为无穷(因为路是无限大的,可以容纳任意多的牛)。s->x1,容量为每个牛棚容量为牛棚的牛数量,x2->t,容量为每个牛棚的容量。
一定要把每个点拆成两个点的。如果直接连原图中的点,是不对的。举个例子:1->2->3, 边长度都为1,则1到3是2。如果枚举ans等于1是,会产生1->2,2->3合法,则最后1也可以到3了,这是错的。
代码:
#include <stdio.h>  
#include 
<string.h>  
#include 
<algorithm>
#include 
<iostream>
#define Min(a, b) (a) < (b) ? a : b  
using  namespace std;  
const  int MAXN = 1005;  
const  int MAXM = 210000;  
const  int INF = 1100000000;  
struct  Edge  
{  
    
int  st, ed;  
    
int  next;  
    
int  flow; 
    
int cap; 
}edge[MAXM]; 
int  head[MAXN], level[MAXN], que[MAXN], E;
//const __int64 MAXLEN = 1000000000;
const __int64 MAXLEN = 9999999999999999;
__int64 map[MAXN][MAXN];
int fieldcow[MAXN], fieldcap[MAXN];
void  add(int u, int v, int w)  
{  
    
//printf("add %d %d %d\n", u, v, w);
    edge[E].flow = 0;  
    edge[E].cap 
= w;
    edge[E].st 
= u;  
    edge[E].ed 
= v;  
    edge[E].next 
= head[u];  
    head[u] 
= E++;      
    edge[E].flow 
= 0
    edge[E].cap 
= 0
    edge[E].st 
= v;  
    edge[E].ed 
= u;  
    edge[E].next 
= head[v];  
    head[v] 
= E++;  
}
int  dinic_bfs(int src, int dest, int ver)        
{        
    
int i, j;         
    
for (i = 0; i <= ver; i++)
    {    
        level[i] 
= -1;
    }
    
int rear = 1;        
    que[
0= src; level[src] = 0;        
    
for(i = 0; i < rear; i++
    {        
          
for(j = head[que[i]]; j != -1; j = edge[j].next)
         {        
            
if(level[edge[j].ed] == -1 && edge[j].cap > edge[j].flow)        
            {        
              level[edge[j].ed] 
= level[que[i]]+1;        
              que[rear
++= edge[j].ed;        
            }
         }
    }
    
return  level[dest] >= 0;        
}        
     
int dinic_dfs(int src, int dest, int ver)        
{        
    
int stk[MAXN], top = 0;        
    
int ret = 0, cur, ptr, pre[MAXN], minf, i;        
    
int del[MAXN];        
    
for (i = 0; i <= ver; i++
    {
        del[i] 
= 0;
    }
    stk[top
++= src;         
    pre[src] 
= src; 
    cur 
= src;        
    
while(top)        
    {        
        
while(cur != dest && top)        
        {        
            
for(i = head[cur]; i != -1; i = edge[i].next)        
            {        
                
if(level[edge[i].ed] == level[cur] + 1 && edge[i].cap > edge[i].flow  && !del[edge[i].ed])        
                {        
                    stk[top
++= edge[i].ed;      
                    cur 
= edge[i].ed;        
                    pre[edge[i].ed] 
= i;                       
                    
break;     
                }        
            }     
            
if(i == -1)       
            {        
                del[cur] 
= 1;        
                top
--;        
                
if(top) cur = stk[top-1];        
            }        
        }                
        
if(cur == dest)        
        {       
            minf 
= INF;        
            
while(cur != src)        
            {        
                cur 
= pre[cur];        
                
if(edge[cur].cap - edge[cur].flow < minf) minf = edge[cur].cap - edge[cur].flow;        
                cur 
= edge[cur].st;        
            }
            cur 
= dest;        
            
while(cur != src)        
            {        
                cur 
= pre[cur];        
                edge[cur].flow 
+= minf;        
                edge[cur
^1].flow -= minf;        
                
if(edge[cur].cap - edge[cur].flow == 0)
                {
                     ptr 
= edge[cur].st;
                }
                cur 
= edge[cur].st;        
            }        
            
while(top > 0&& stk[top-1!= ptr) top--;        
            
if(top)  cur = stk[top-1];        
            ret 
+= minf;      
        }        
    }        
    
return ret;        
}        
int Dinic(int src, int dest, int ver)        
{        
    
int  ret = 0, t;        
    
while(dinic_bfs(src, dest, ver))        
    {        
        t 
= dinic_dfs(src, dest, ver);        
        
if(t) ret += t;        
        
else  break;        
    }        
    
return ret;        
}
void floyd(int n)
{
    
int k, i, j;
    
for (k = 1; k <= n; k++)
    {
        
for (i = 1; i <= n; i++)
        {
            
for (j = 1; j <= n; j++)
            {
                
//可以自己到自己的。因为自己对自己可容纳牛。 
                if (map[i][k] != MAXLEN && map[k][j] != MAXLEN && map[i][k] + map[k][j] < map[i][j])
                {
                    map[i][j] 
= map[i][k] + map[k][j];
                }
            }
        }
    }
}
void build(int n, __int64 distance)
{
    
int s = 0, t = n + n + 1, ver = t + 1, i, j;
    E 
= 0;
    
for (i = 0; i <= ver; i++)
    {
        head[i] 
= -1;
    }
    
for (i = 1; i <= n; i++)
    {
        
for (j = 1; j <= n; j++)
        {
            
if (map[i][j] <= distance)
            {
                add(i, j 
+ n, INF);
            }
        }
    }
    
for (i = 1; i <= n; i++)
    {
        add(
0, i, fieldcow[i]);
        add(i 
+ n, t, fieldcap[i]);
    }
}
int main()
{
    
int n, m, i, cownum, j, u, v;
    __int64 w, limit 
= 0;
    scanf(
"%d%d"&n, &m);
    
for (i = 1, cownum = 0; i <= n; i++)
    {
        scanf(
"%d%d"&fieldcow[i], &fieldcap[i]);
        cownum 
+= fieldcow[i];
    }
    
for (i = 1; i <= n; i++)
    {
        
for (j = 1; j <= n; j++)
        {
            map[i][j] 
= MAXLEN;            
        }
        map[i][i] 
= 0;
    }
    
while (m--)
    {
        scanf(
"%d%d%I64d"&u, &v, &w);
        limit 
+= w;
        
if (map[u][v] > w)
        {
            map[u][v] 
= map[v][u] = w;
        }
    }
    floyd(n);
    
int s = 0, t = n + n + 1, ver = t + 1, ans, sign = 0;
    __int64 l 
= 0, r = limit + 1, mid;
    
while (l < r)
    {
        mid 
= (l + r) >> 1;
         build(n, mid);
         ans 
= Dinic(0, t, ver);
         
if (ans == cownum)
         {
             r 
= mid;
             sign 
= 1;
         }
         
else
         {
             l 
= mid + 1;
         }
    }
    
if (sign)//如果没有满足所有牛都进牛棚会输出-1 
    {
        printf(
"%I64d\n", r);
    }
    
else
    {
        printf(
"-1\n");
    }
    
return 0;
}
/*
2 1
1 2
2 0
1 2 10

3 1
1 2
1 0
3 3
1 2 10

3 1
1 2
1 0
3 2
1 2 10

3 4
7 2
0 4
2 6
1 2 40
3 2 70
2 3 90
1 3 120

1 0
1 2

1 0
2 1
*/