ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

UVa 11090 - Going in Cycle!! (01分数规划)

2021-07-18 16:35:53  阅读:221  来源: 互联网

标签:cnt ch 11090 01 int double mid Going maxn


题目链接:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=0&problem=2031&mosmsg=Submission+received+with+ID+26584370

求有向图平均权最小的回路

二分答案 \(x\),将所有边都减去 \(x\),看图中是否有负环即可

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn = 105;
const double eps = 1e-3;

int T, n, m; 

int h[maxn], cnt = 0;
struct E{
	int to, next;
	double cost;
}e[maxn * maxn];
void add(int u, int v, double w){
	e[++cnt].to = v;
	e[cnt].cost = w;
	e[cnt].next = h[u];
	h[u] = cnt;
}

int in[maxn], tot[maxn]; double d[maxn];
bool spfa(double mid){
	queue<int> q;
	memset(tot, 0, sizeof(tot));
	memset(in, 0, sizeof(in));
	for(int i = 1 ; i <= n ; ++i) {
		d[i] = 0;
		in[i] = 1;
		q.push(i);
	}
	
	while(!q.empty()){
		int u = q.front(); q.pop(); in[u] = 0;
		for(int i = h[u] ; i != -1 ; i = e[i].next){
			int v = e[i].to;
			if(d[u] + e[i].cost - mid - d[v] < 0){
				d[v] = d[u] + e[i].cost - mid;
				
				if(!in[v]){
					in[v] = 1;
					q.push(v);
					++tot[v];
					if(tot[v] > n) return false;
				}
			}
		}
	}
	
	return true;
}

bool check(double x){
	return !spfa(x);
}

ll read(){ ll s = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); } return s * f; }

int main(){
	scanf("%d", &T);
	int kase = 0;
	while(T--){
		memset(h, -1, sizeof(h)); cnt = 0;
		scanf("%d%d", &n, &m);
		int u, v; double w;
		double L = 0, R = 0;
		for(int i = 1 ; i <= m ; ++i){
			scanf("%d%d%lf", &u, &v, &w);
			add(u, v, w);
			R = max(R, w * 1.0);
		}
		
		printf("Case #%d: ", ++kase);	
		if(!check(R + 1.0)) printf("No cycle found.\n");
		else {
			double ans = 0;
			while(R - L > eps){
				double mid = (R + L) / 2;
				if(check(mid)){
					R = mid;
				} else{
					L = mid;
				}
			}
			printf("%.2lf\n", L);
		}
	}
	return 0;
}

标签:cnt,ch,11090,01,int,double,mid,Going,maxn
来源: https://www.cnblogs.com/tuchen/p/15026889.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有