Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10620 | Accepted: 3110 |
Description
Input
Output
Sample Input
7 9 2 1 2 2 2 3 5 3 7 5 1 4 1 4 3 1 4 5 7 5 7 1 1 6 3 6 7 3
Sample Output
5
Hint
Huge input data,scanf is recommended.
好久没有1A了。。。
题意:给你一个N个点(编号从1到N)和M条边的无向图以及每条边的权值。要求从1到N至少要有T条边不重复的路径,让你在满足这个前提下求出所有路径(当然是选出的那些边不重复的路径,不算没有选上的)上的最大边权值的 最小值。题目保证从1到N至少会有T条边不重复的路径,也就是说,题目一定有解。
思路:二分枚举所有被选中路径上 最大边权值mid,判断在mid值的限制下从1到N是否存在至少T条边不重复的路径。
边不重复的路径数目可以用最大流求解——枚举原图中所有无向边,若边权不大于mid,就将该边加进新图且边的容量为1。最后从1到N跑一下最大流就行了。
AC代码:
#include <cstdio> #include <cstring> #include <queue> #include <stack> #include <vector> #include <algorithm> #define MAXN 200+10 #define MAXM 1600000+10 #define INF 0x3f3f3f3f using namespace std; struct Edge { int from, to, cap, flow, next; }; Edge edge[MAXM], Redge[MAXM]; int head[MAXN], edgenum, Rhead[MAXN], Redgenum; int dist[MAXN], cur[MAXN]; bool vis[MAXN]; int N, M, T; struct Node { int from, to, val, next; }; Node node[MAXM]; int Head[MAXN], nodenum; void addNode(int u, int v, int w) { Node E = {u, v, w, Head[u]}; node[nodenum] = E; Head[u] = nodenum++; } int Max;//记录最大边权 void input() { memset(Head, -1, sizeof(Head)); nodenum = 0; Max = 0; int a, b, c; for(int i = 1; i <= M; i++) { scanf("%d%d%d", &a, &b, &c); Max = max(Max, c);//更新 addNode(a, b, c); addNode(b, a, c); } } void init() { edgenum = 0; memset(head, -1, sizeof(head)); } void addEdge(int u, int v, int w) { Edge E1 = {u, v, w, 0, head[u]}; edge[edgenum] = E1; head[u] = edgenum++; Edge E2 = {v, u, 0, 0, head[v]}; edge[edgenum] = E2; head[v] = edgenum++; } void getMap(int mid) { for(int i = 0; i < nodenum; i++) { if(node[i].val <= mid)//小于或等于限制 addEdge(node[i].from, node[i].to, 1);//加入新图 } } bool BFS(int s, int t) { queue<int> Q; memset(dist, -1, sizeof(dist)); memset(vis, false, sizeof(vis)); dist[s] = 0; vis[s] = true; Q.push(s); while(!Q.empty()) { int u = Q.front(); Q.pop(); for(int i = head[u]; i != -1; i = edge[i].next) { Edge E = edge[i]; if(!vis[E.to] && E.cap > E.flow) { dist[E.to] = dist[u] + 1; vis[E.to] = true; if(E.to == t) return true; Q.push(E.to); } } } return false; } int DFS(int x, int a, int t) { if(x == t || a == 0) return a; int flow = 0, f; for(int &i = cur[x]; i != -1; i = edge[i].next) { Edge &E = edge[i]; if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap-E.flow), t)) > 0) { edge[i].flow += f; edge[i^1].flow -= f; flow += f; a -= f; if(a == 0) break; } } return flow; } int Maxflow(int s, int t) { int flow = 0; while(BFS(s, t)) { memcpy(cur, head, sizeof(head)); flow += DFS(s, INF, t); } return flow; } int main() { while(scanf("%d%d%d", &N, &M, &T) != EOF) { input(); int left = 0, right = Max, ans = Max; while(right >= left)//二分查找 { int mid = (left + right) >> 1; init(); getMap(mid); if(Maxflow(1, N) >= T)//判断是否存在T条边不重复的路径 { ans = min(ans, mid);//更新 right = mid - 1; } else left = mid + 1; } printf("%d\n", ans); } return 0; }