处理重边,想不出什么好的方法。。只好用了前向星来写邻接表存储。。。。不是很难不多说什么了。。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxM = 80005;
const int inf = INT_MAX;
#define CC(m,v) memset(m,v,sizeof(m))
struct node {
int u, v, f, next;
} edge[maxM];
int head[maxM], p = 0, lev[maxM], val[maxM], cur[maxM];
int que[maxM];
inline void init1() {
p = 0, CC(edge, 0), CC(head, -1), CC(val, 0);
}
inline void addedge(int u, int v, int f) { // 无向图
edge[p].u = u, edge[p].v = v, val[p] = f, edge[p].next = head[u], head[u] = p++;
edge[p].u = v, edge[p].v = u, val[p] = f, edge[p].next = head[v], head[v] = p++;
}
bool bfs(int s, int t) {
int fir, en, u, v, i, j;
memset(lev, 0, sizeof (lev));
lev[s] = 1, que[0] = s, fir = 0, en = 1;
while (fir != en) {
u = que[fir++];
for (i = head[u]; i != -1; i = edge[i].next)
if (edge[i].f > 0 && lev[v = edge[i].v] == 0) {
lev[v] = lev[u] + 1, que[en++] = v;
if (v == t) {
fir = en;
break;
}
}
}
return lev[t];
}
int dinic(int s, int t) {
int u, v, i, j, k, iq, f;
int flow = 0;
while (bfs(s, t)) {
memcpy(cur, head, sizeof (head));
u = s, iq = 0;
while (1) {
if (u == t) {
for (k = 0, f = inf; k < iq; k++)
if (edge[que[k]].f < f)
f = edge[que[k]].f, j = k;
for (k = 0; k < iq; k++)
edge[que[k]].f -= f, edge[que[k]^1].f += f;
flow += f, u = edge[que[iq = j]].u;
}
for (j = cur[u]; cur[u] != -1; j = cur[u] = edge[cur[u]].next)
if (edge[j].f > 0 && lev[u] + 1 == lev[edge[j].v]) break;
if (cur[u] != -1) {
que[iq++] = cur[u];
u = edge[cur[u]].v;
} else {
if (iq == 0) break;
lev[u] = -1;
u = edge[que[--iq]].u;
}
}
}
return flow;
}
int main() {
int ni, pi, ti, ai, bi, li;
int i, j, lef, rig, mid;
while (scanf("%d%d%d", &ni, &pi, &ti) != -1) {
init1();
lef = inf, rig = -1;
for (i = 0; i < pi; i++) {
scanf("%d%d%d", &ai, &bi, &li);
addedge(ai, bi, li);
if (li < lef) lef = li;
if (li > rig) rig = li;
}
while (lef <= rig) {
mid = (lef + rig) / 2;
for (i = 0; i < p; i++) {
if (val[i] <= mid)
edge[i].f = 1;
else
edge[i].f = 0;
}
if (dinic(1, ni) >= ti) rig = mid - 1;
else lef = mid + 1;
}
printf("%d\n", lef);
}
return 0;
}