• UVA 11248


    UVA 11248 - Frequency Hopping

    题目链接

    题意:给定一个网络,如今须要从1到N运输流量C,问是否可能,假设可能输出可能,假设不可能,再问能否通过扩大一条边的容量使得可能,假设能够输出这些边(按u先排再按v排),假设不行输出不可能

    思路:先做一遍网络流,然后每次在最小割上进行添加容量,须要两个优化,每次找流量找到>= c就能够了,然后每次改动容量,能够直接从之前做过的网络流继续做就可以

    代码:

    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    using namespace std;
    
    const int MAXNODE = 105 * 2;
    const int MAXEDGE = 100005;
    
    typedef int Type;
    const Type INF = 0x3f3f3f3f;
    
    int n, m, c;
    
    struct Edge {
    	int u, v;
    	Type cap, flow;
    	Edge() {}
    	Edge(int u, int v, Type cap, Type flow) {
    		this->u = u;
    		this->v = v;
    		this->cap = cap;
    		this->flow = flow;
    	}
    };
    
    bool cmp(Edge a, Edge b) {
    	if (a.u != b.u) return a.u < b.u;
    	return a.v < b.v;
    }
    
    struct Dinic {
    	int n, m, s, t;
    	Edge edges[MAXEDGE], etmp[MAXEDGE];
    	int first[MAXNODE];
    	int next[MAXEDGE];
    	bool vis[MAXNODE];
    	Type d[MAXNODE];
    	int cur[MAXNODE];
    	vector<int> cut;
    	vector<Edge> ans;
    	Type flow;
    
    	void init(int n) {
    		this->n = n;
    		memset(first, -1, sizeof(first));
    		m = 0;
    		flow = 0;
    	}
    	void add_Edge(int u, int v, Type cap) {
    		edges[m] = Edge(u, v, cap, 0);
    		next[m] = first[u];
    		first[u] = m++;
    		edges[m] = Edge(v, u, 0, 0);
    		next[m] = first[v];
    		first[v] = m++;
    	}
    
    	bool bfs() {
    		memset(vis, false, sizeof(vis));
    		queue<int> Q;
    		Q.push(s);
    		d[s] = 0;
    		vis[s] = true;
    		while (!Q.empty()) {
    			int u = Q.front(); Q.pop();
    			for (int i = first[u]; i != -1; i = next[i]) {
    				Edge& e = edges[i];
    				if (!vis[e.v] && e.cap > e.flow) {
    					vis[e.v] = true;
    					d[e.v] = d[u] + 1;
    					Q.push(e.v);
    				}
    			}
    		}
    		return vis[t];
    	}
    
    	Type dfs(int u, Type a) {
    		if (u == t || a == 0) return a;
    		Type flow = 0, f;
    		for (int &i = cur[u]; i != -1; i = next[i]) {
    			Edge& e = edges[i];
    			if (d[u] + 1 == d[e.v] && (f = dfs(e.v, min(a, e.cap - e.flow))) > 0) {
    				e.flow += f;
    				edges[i^1].flow -= f;
    				flow += f;
    				a -= f;
    				if (a == 0) break;
    			}
    		}
    		return flow;
    	}
    
    	void MinCut() {
    		cut.clear();
    		for (int i = 0; i < m; i += 2) {
    			if (vis[edges[i].u] && !vis[edges[i].v])
    				cut.push_back(i);
    		}
    	}
    
    	bool Maxflow(int s, int t) {
    		this->s = s; this->t = t;
    		while (bfs()) {
    			for (int i = 0; i < n; i++)
    				cur[i] = first[i];
    			flow += dfs(s, INF);
    			if (flow >= c) return true;
    		}
    		return false;
    	}
    
    	void solve() {
    		if (Maxflow(0, n - 1) || c == 0) printf("possible
    ");
    		else {
    			MinCut();
    			ans.clear();
    			for (int i = 0; i < m; i++)
    				etmp[i] = edges[i];
    			int tmpf = flow;
    			for (int i = 0; i < cut.size(); i++) {
    				edges[cut[i]].cap = edges[cut[i]].flow + c;
    				if (Maxflow(0, n - 1)) ans.push_back(edges[cut[i]]);
    				flow = tmpf;
    				for (int i = 0; i < m; i++)
    					edges[i] = etmp[i];
    			}
    			if (ans.size() == 0) printf("not possible
    ");
    			else {
    				sort(ans.begin(), ans.end(), cmp);
    				printf("possible option:");
    				for (int i = 0; i < ans.size(); i++)
    					printf("(%d,%d)%c", ans[i].u + 1, ans[i].v + 1, i == ans.size() - 1 ? '
    ' : ',');
    			}
    		}
    	}
    } gao;
    
    int main() {
    	int cas = 0;
    	while (~scanf("%d%d%d", &n, &m, &c) && n) {
    		gao.init(n);
    		int u, v, cap;
    		while (m--) {
    			scanf("%d%d%d", &u, &v, &cap);
    			u--; v--;
    			gao.add_Edge(u, v, cap);
    		}
    		printf("Case %d: ", ++cas);
    		gao.solve();
    	}
    	return 0;
    }


    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    Sightseeing,题解
    A Simple Problem,题解
    城池攻占,题解
    传递,题解
    How many ways??,题解
    Least Cost Bracket Sequence,题解
    Evacuation,题解
    Tallest Cow,题解
    容易题,题解
    无题Ⅱ,题解
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4884345.html
Copyright © 2020-2023  润新知