Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Dinic+二分,1A留念#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<queue> using namespace std; const int oo =0x3f3f3f3f; int V, E, T; struct edge{ int to, cap, rev; }; vector<edge> G[200]; int iter[200]; int level[200]; void add_edge(int from, int to, int cap){ G[from].push_back((edge) {to, cap, G[to].size()}); G[to].push_back((edge) {from, 0, G[from].size() - 1}); } void bfs(int s){ memset(level, -1, sizeof(level)); level[s] = 0; queue<int> que; que.push(s); while(!que.empty()){ int v = que.front(); que.pop(); for(int i =0; i < G[v].size(); i ++){ edge e = G[v][i]; if(e.cap > 0 && level[e.to] < 0){ level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v, int t, int f){ if(v == t){ return f; } for(int & i = iter[v]; i < G[v].size(); i ++){ edge & e = G[v][i]; if(e.cap > 0 &&level[e.to] > level[v]){ int d = dfs(e.to, t, min(f, e.cap)); if(d > 0){ e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t){ int flow = 0; while(true){ bfs(s); if(level[t] < 0){ return flow; } memset(iter, 0, sizeof(iter)); int f; while((f = dfs(s, t, oo)) > 0){ flow += f; } } } int A[40000], B[40000], C[40000]; int Judge(int mid){ for(int i =0; i < V; i ++){ G[i].clear(); } for(int i = 0; i < E; i ++){ if(C[i] <= mid){ add_edge(A[i], B[i], 1); add_edge(B[i], A[i], 1); } } return max_flow(0, V - 1); } int main(){ scanf("%d %d %d", &V, &E, &T); int max_len = 0; for(int i =0; i < E; i ++){ scanf("%d %d %d", A + i, B + i, C + i); A[i] --; B[i] --; max_len = max(max_len, C[i]); } int lb = 0, ub = max_len; int ans =-1; while(ub >= lb){ int mid = (ub + lb) >> 1; if(Judge(mid) >= T){ ans = mid; ub = mid - 1; } else { lb = mid + 1; } } printf("%d\n", ans); } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator