1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <set> #include <vector> using namespace std; typedef long long ll; long long INF = 1e17; int inf = 1e9; typedef pair <int,int> P; #define MID(x,y) ((x+y)>>1) #define iabs(x) ((x)>0?(x):-(x)) #define REP(i,a,b) for(int i=(a);i<(b);i++) #define FOR(i,a,b) for(int i=(a);i<=(b);i++) #define pb push_back #define mp make_pair #define print() cout<<"--------"<<endl #define maxn 500
struct Edge{ int to,cap,flow; Edge(){} Edge(int a,int b,int c){ to = a; cap = b; flow = c; } };
struct Dinic{ int n,m,s,t; vector<Edge> edg; vector<int> g[maxn]; bool vis[maxn]; int d[maxn]; int cur[maxn];
void init(int N){ this->n = N; edg.clear(); for (int i = 0; i <= n; i ++){ g[i].clear(); } memset(d,0,sizeof(d)); }
void addEdge(int from,int to,int cap){ edg.pb(Edge(to,cap,0)); edg.pb(Edge(from,0,0)); m = edg.size(); g[from].pb(m - 2); g[to].pb(m - 1); }
bool bfs(){ memset(vis,0,sizeof(vis)); queue<int> q; q.push(s); d[s] = 0; vis[s] = 1; while (!q.empty()){ int u = q.front();q.pop(); for (int i = 0; i < g[u].size(); i ++){ Edge& e = edg[g[u][i]]; if (!vis[e.to] && e.cap > e.flow){ vis[e.to] = 1; d[e.to] = d[u] + 1; q.push(e.to); } } } return vis[t]; }
int dfs(int u,int a){ if (u == t || a == 0) return a; int flow = 0, f; for (int& i = cur[u]; i < g[u].size(); i ++){ Edge& e = edg[g[u][i]]; if (d[u] + 1 == d[e.to] && (f = dfs(e.to, min(a,e.cap - e.flow))) > 0){ e.flow += f; edg[g[u][i]^1].flow -= f; flow += f; a -= f; if (a == 0) break; } } return flow; }
int maxFlow(int s,int t){ this->s = s; this->t = t; int flow = 0; while (bfs()){ memset(cur, 0, sizeof(cur)); flow += dfs(s, inf); } return flow; } }G;
#define maxp 500 ll maps[maxp][maxp]; int n,m; int c[maxp],w[maxp]; void floyd(){ for (int k = 1; k <= n; k ++){ for (int i = 1; i <= n; i ++){ for (int j = 1; j <= n; j ++){ if (maps[i][k] != INF && maps[k][j] != INF){ maps[i][j] = min(maps[i][j], maps[i][k] + maps[k][j]); } } } } }
int sum; bool ok(ll mid){ G.init(2 * n + 10); int st = 2 * n + 1,ed = 2 * n + 2; for (int i = 1; i <= n; i ++){ G.addEdge(st, i, c[i]); } for (int i = 1; i <= n; i ++){ G.addEdge(i + n, ed, w[i]); } for (int i = 1; i <= n; i ++){ for (int j = 1; j <= n; j ++){ if (maps[i][j] <= mid){ G.addEdge(i, j + n, inf); } } } return sum == G.maxFlow(st,ed); }
int main(){ while (~scanf("%d%d", &n, &m)){ sum = 0; for (int i = 1; i <= n; i ++){ scanf("%d%d", &c[i], &w[i]); sum += c[i]; } for (int i = 1; i <= n; i ++){ for (int j = 1; j <= n; j ++){ maps[i][j] = INF; } } for (int i = 1; i <= n; i ++) maps[i][i] = 0; int u,v,len; ll le = 0,ri = 0; for (int i = 1; i <= m; i ++){ scanf("%d%d%d", &u, &v, &len); ri += len; if (maps[u][v] > len) maps[u][v] = maps[v][u] = len; } floyd();
ll ans = -1; while (le <= ri){ ll mid = MID(le,ri); if (ok(mid)){ ri = mid - 1; ans = mid; }else { le = mid + 1; } } printf("%lldn", ans); } return 0; }
|