zoj 2676 Network Wars

Network Wars

Time Limit: 5 Seconds       Memory Limit: 32768 KB       Special Judge

Network of Byteland consists of n servers, connected by m optical cables. Each cable connects two servers and can transmit data in both directions. Two servers of the network are especially important --- they are connected to global world network and president palace network respectively.

The server connected to the president palace network has number 1, and the server connected to the global world network has number n.

Recently the company Max Traffic has decided to take control over some cables so that it could see what data is transmitted by the president palace users. Of course they want to control such set of cables, that it is impossible to download any data from the global network to the president palace without transmitting it over at least one of the cables from the set.

To put its plans into practice the company needs to buy corresponding cables from their current owners. Each cable has some cost. Since the company's main business is not spying, but providing internet connection to home users, its management wants to make the operation a good investment. So it wants to buy such a set of cables, that cables mean cost} is minimal possible.

That is, if the company buys k cables of the total cost c, it wants to minimize the value of c/k.

Input

There are several test cases in the input. The first line of each case contains  n  and  m  ( 2 <= n <= 100  ,  1 <= m <= 400  ). Next  m  lines describe cables~--- each cable is described with three integer numbers: servers it connects and the cost of the cable. Cost of each cable is positive and does not exceed  107 .

Any two servers are connected by at most one cable. No cable connects a server to itself. The network is guaranteed to be connected, it is possible to transmit data from any server to any other one.

There is an empty line between each cases.

Output

First output  k  --- the number of cables to buy. After that output the cables to buy themselves. Cables are numbered starting from one in order they are given in the input file. There should an empty line between each cases.

Example

InputOutput
6 8
1 2 3
1 3 3
2 4 2
2 5 2
3 4 2
3 5 2
5 6 3
4 6 3
4
3 4 5 6 
4 5
1 2 2
1 3 2
2 3 1
2 4 2
3 4 2
3
1 2 3


此题同样是分数规划,还要注意此题的图是无向图,无向图只需要把每条边的正向弧和反向弧都初始为容量即可。

算法思路参考《最小割模型在信息学竞赛中的应用》作者胡伯涛


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits>
#include <set>

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;

const int MAXN(110);
const int MAXE(410);
const double INFI(1e11);
const double EPS(1e-7);

int first[MAXN];
int u[MAXE << 1], v[MAXE << 1], num[MAXE << 1], next[MAXE << 1];
double lf[MAXE << 1];
int rear;
bool is_ans[MAXE];

inline void init()
{
	memset(first, -1, sizeof(first));
	rear = 0;
}

inline void insert(int tu, int tv, double tc, int tn)
{
	u[rear] = tu;
	v[rear] = tv;
	lf[rear] = tc;
	num[rear] = tn;
	next[rear] = first[tu];
	first[tu] = rear++;
	u[rear] = tv;
	v[rear] = tu;
	lf[rear] = tc;
	next[rear] = first[tv];
	first[tv] = rear++;
}

int N, M;
int edge[MAXE][3];
int level[MAXN];

bool bfs()
{
	memset(level, -1, sizeof(level));
	level[1] = 0;
	queue<int> que;
	que.push(1);
	while(!que.empty())
	{
		int cur = que.front();
		que.pop();
		for(int i = first[cur]; i != -1; i = next[i])
			if(lf[i] > 0)
			{
				int tv = v[i];
				if(level[tv] == -1)
				{
					level[tv] = level[cur]+1;
					que.push(tv);
				}
			}
	}
	return level[N] != -1;
}

double dfs(int cur, double limit)
{
	if(cur == N)
		return limit;
	double tf = 0;
	for(int i = first[cur]; i != -1; i = next[i])
		if(lf[i] > 0)
		{
			int tv = v[i];
			if(level[tv] == level[cur]+1)
			{
				double temp = dfs(tv, min(lf[i], limit-tf));
				tf += temp;
				lf[i] -= temp;
				lf[i^1] += temp;
			}
		}
	if(tf == 0.)
		level[cur] = -1;
	return tf;
}


double dinic()
{
	double ret = 0;
	while(bfs())
	{
		ret += dfs(1, INFI);
	}
	return ret;
}

double fun(double m)
{
	init();
	double ret = 0;
	memset(is_ans, 0, sizeof(is_ans));
	for(int i = 1; i <= M; ++i)
	{
		double temp = edge[i][2]-m;
		if(temp <= 0)
		{
			ret += temp;
			is_ans[i] = true;
		}
		else
			insert(edge[i][0], edge[i][1], temp, i);
	}
	ret += dinic();
	return ret;
}

int main()
{
	while(~scanf("%d%d", &N, &M))
	{
		double l = 0;
		double r = 0;
		for(int i = 1; i <= M; ++i)
		{
			scanf("%d%d%d", edge[i], edge[i]+1, edge[i]+2);
			r += edge[i][2];
		}
		r /= M;
		while(r-l > EPS)
		{
			double m = (l+r)/2.;
			if(fun(m) < 0)
			{
				r = m;
			}
			else
			{
				l = m;
			}
		}
		for(int i = 0; i < rear; i += 2)
			if(level[u[i]] == -1)
			{
				if(level[v[i]] != -1)
					is_ans[num[i]] = true;
			}
			else
			{
				if(level[v[i]] == -1)
					is_ans[num[i]] = true;
			}
		queue<int> temp;
		for(int i = 1; i <= M; ++i)
			if(is_ans[i])
				temp.push(i);
		printf("%d\n", temp.size());
		while(!temp.empty())
		{
			printf("%d ", temp.front());
			temp.pop();
		}
		printf("\n\n");
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值