2017 Bubble Cup X - Finals D. Exploration plan(最短路+二分+网络流)

比赛时候写不出来,因为发现费用流是无法求出每次的时间的,于是绝望中挂机。

题意:有V个城市,他们由E条路相连。有N个队伍,他们一开始在某些城市,现在要求队伍所覆盖的城市数目不能小于K。求出能完成这个目标的最小时间。

然后看了一发题解,大意就是:

1.先每个点跑一次最短路,然后构出一个每个点到另外一个点的距离的图。

2.然后二分时间,下界为0,上界为稍微大于所给时间的值。

3.每次得到mid后建图,源点为0,N个队伍分别是1……N,源点向每个队伍连接一条流量为1的边,每个队伍所在的城市for一遍到其他城市的距离,如果小于等于mid则连接一条当前队伍i到N + j(城市下标)流量为1的边。最后每个城市向汇点连接一条流量为1的边。

4.跑一遍网络流,看所覆盖城市是否大于等于K,不断二分。

时间复杂度为最短路的 V^2*logE + (二分+网络流的) log(maxtime) * (N + V) ^ 2 * (N * V). 当然,dinic的复杂度是玄学就是了。

代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
const int maxn = 1010;
const int maxm = 1e6 + 5;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int, int> pii;
inline int read()
{
	int x=0,t=1,c;
	while(!isdigit(c=getchar()))if(c=='-')t=-1;
	while(isdigit(c))x=x*10+c-'0',c=getchar();
	return x*t;
}
int V, E, N, K;
int head[maxn],cur[maxn],nx[maxm<<1],to[maxm<<1],flow[maxm<<1],cost[maxm<<1],ppp=0;
int G[maxn][maxn], dis[maxn];
struct Dinic
{
	
	int s, t;
	long long ans;
	
	void init() {
		memset(head, -1, sizeof(head));
		ppp = 0;
	}
	
	void AddEdge(int u, int v, int c) {
		to[ppp]=v;flow[ppp]=c;nx[ppp]=head[u];head[u]=ppp++;swap(u,v);
		to[ppp]=v;flow[ppp]=0;nx[ppp]=head[u];head[u]=ppp++;
	}
	
	bool BFS() {
		memset(dis, -1, sizeof(dis));
		dis[s] = 1; 
		queue<int> Q;
		Q.push(s);
		while(!Q.empty()) {
			int x = Q.front();
			Q.pop();
			for(int i = head[x]; ~i; i = nx[i]) {
				if(flow[i] && dis[to[i]] == -1) {
					dis[to[i]] = dis[x] + 1;
					Q.push(to[i]);
				}
			}
		}
		return dis[t] != -1;
	}
	
	int DFS(int x, int maxflow) {
		if(x == t || !maxflow){
			ans += maxflow;
			return maxflow;
		}
		int ret = 0, f;
		for(int &i = cur[x]; ~i; i = nx[i]) {
			if(dis[to[i]] == dis[x] + 1 && (f = DFS(to[i], min(maxflow, flow[i])))) {
				ret += f;
				flow[i] -= f;
				flow[i^1] += f;
				maxflow -= f;
				if(!maxflow)
					break;
			}
		}
		return ret;
	}
	
	long long solve(int source, int tank) {
		s = source;
		t = tank;
		ans = 0;
		while(BFS()) {
			memcpy(cur, head, sizeof(cur));
			DFS(s, INF);
		}
		return ans;
	}
}dinic;

void add_edge(int u, int v, int val) {
	nx[ppp] = head[u], cost[ppp] = val, to[ppp] = v, head[u] = ppp++;
	swap(u, v);
	nx[ppp] = head[u], cost[ppp] = val, to[ppp] = v, head[u] = ppp++;
}

void dijkstra(int s) {
	memset(dis, 0x3f, sizeof(dis));
	priority_queue <pii, vector<pii>, greater<pii> > q;
	dis[s] = 0;
	q.push(pii(dis[s], s));
	while(!q.empty()) {
		pii tmp = q.top();
		q.pop();
		int u = tmp.second;
		if(tmp.first > dis[u])
			continue;
		for(int i = head[u]; ~i; i = nx[i]) {
			int v = to[i];
			int d = cost[i] + dis[u];
			if(dis[v] > d) {
				dis[v] = d;
				q.push(pii(dis[v], v));
			}
		}
	}
}

int team[205];

void build(int mid, int s, int t) {
	dinic.init();
	for(int i = 1; i <= N; i++) {
		int tmp = team[i];
		dinic.AddEdge(s, i, 1);
		for(int j = 1; j <= V; j++) {
			if(G[tmp][j] <= mid) 
				dinic.AddEdge(i, N + j, 1);
		}
	}
	for(int i = 1; i <= V; i++)
		dinic.AddEdge(N + i, t, 1);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
#endif
	V=read(),E=read(),N=read(),K=read();
	for(int i = 1; i <= N; i++) {
		team[i] = read();
	}
	dinic.init();
	for(int i = 0; i < E; i++) {
		int u = read(), v = read(), val = read();
		add_edge(u, v, val);
	}
	for(int i = 1; i <= V; i++) {
		dijkstra(i);
		for(int j = 1; j <= V; j++) {
			G[i][j] = dis[j];
		}
	}
	
	int front = 0, back = 1731313;
	int s = 0, t = N + V + 1;
	while(back > front) {
		int mid = (front + back) / 2;
		build(mid, s, t);
		int ans = dinic.solve(s, t);
		if(ans >= K)
			back = mid;
		else
			front = mid + 1;
	}
	if(front > 1731311)
		printf("-1\n");
	else
		printf("%d\n", front);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Write a Model class with the following UML specification: +----------------------------------------------+ | Model | +----------------------------------------------+ | - score: int | | - bubbles: ArrayList<IShape> | +----------------------------------------------+ | + Model() | | + getScore(): int | | + addBubble(int w, int h): void | | + moveAll(int dx, int dy): void | | + clearInvisibles(int w, int h): void | | + deleteBubblesAtPoint(int x, int y): void | | + drawAll(Graphics g): void | | + testModel(): void | +----------------------------------------------+ When a new model object is created, the score must be zero and the arraylist must be empty. The getScore method returns as result the current score for the game. The addBubble method adds a new bubble to the arraylist of bubbles. The position of the center of the new bubble is random but must be inside a window of width w and height h (the arguments of the addBubble method), like this: new Bubble((int)(w * Math.random()), (int)(h * Math.random())) The moveAll method moves the positions of all the bubbles in the arraylist of bubbles by the amount dx in the x direction and by the amount dy in the y direction. The clearInvisibles method takes as argument the width w and the height h of the window, and deletes from the arraylist of bubbles any bubble which is not visible in the window anymore. For each bubble which is deleted, the score decreases by 1.The deleteBubblesAtPoint method takes as argument the coordinates (x, y) of a point, and deletes from the arraylist of bubbles any bubble which contains this point (multiple bubbles might contain the point, because bubbles can overlap in the window). For each bubble which is deleted, the score increases by 1. The drawAll method draws all the bubbles in the arraylist of bub
最新发布
05-11

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值