Going in Cycle!! UVA - 11090(二分+判断环路 )
题意:
给定一个n个点m条边的加权有向图,求平均权值最小的回路
解析:
首先肯定是想到找出环路 然后。。呵。。呵。。呵呵。。。
显然不现实!!
二分大法好 。。。。去猜结果 然后带入验证 。。。真是的。。很过分!
嗯! 是的!
我参考一下UVA11478的代码 。。。建立超级源的做法。。竟然50ms 网上的用遍历每个没经过的点的做法2130ms 质的飞跃 。。。。。
- #include <iostream>
- #include <cstdio>
- #include <sstream>
- #include <cstring>
- #include <map>
- #include <set>
- #include <vector>
- #include <stack>
- #include <queue>
- #include <algorithm>
- #include <cmath>
- #define rap(i, a, n) for(int i=a; i<=n; i++)
- #define MOD 2018
- #define LL long long
- #define ULL unsigned long long
- #define Pair pair<int, int>
- #define mem(a, b) memset(a, b, sizeof(a))
- #define _ ios_base::sync_with_stdio(0),cin.tie(0)
- //freopen("1.txt", "r", stdin);
- using namespace std;
- const int maxn = , INF = 0x7fffffff;
- int head[maxn], vis[maxn], ans[maxn];
- double d[maxn];
- int cnt, n, m;
- struct node
- {
- int v, next;
- double w;
- }Node[maxn];
- void add(int u, int v, double w)
- {
- Node[cnt].v = v;
- Node[cnt].w = w;
- Node[cnt].next = head[u];
- head[u] = cnt++;
- }
- int spfa()
- {
- queue<int> Q;
- for(int i=; i<=n; i++)
- {
- Q.push(i);
- d[i] = ;
- vis[i] = ;
- }
- mem(ans, );
- while(!Q.empty())
- {
- int u = Q.front(); Q.pop();
- vis[u] = ;
- for(int i=head[u]; i!=-; i=Node[i].next)
- {
- node e = Node[i];
- if(d[e.v] > d[u] + e.w)
- {
- d[e.v] = d[u] + e.w;
- if(!vis[e.v])
- {
- Q.push(e.v);
- vis[e.v] = ;
- if(++ans[e.v] >= n) return ;
- }
- }
- }
- }
- return ;
- }
- bool check(double x)
- {
- bool flag = ;
- for(int i=; i<cnt; i++)
- Node[i].w -= x;
- // for(int i=1; i<=n; i++)
- // if(spfa(i))
- // flag = 1;
- if(spfa())
- flag = ;
- for(int i=; i<cnt; i++)
- Node[i].w += x;
- return flag;
- }
- void init()
- {
- mem(head, -);
- cnt = ;
- }
- int main()
- {
- int T, kase = ;
- scanf("%d", &T);
- while(T--)
- {
- init();
- int u, v;
- double w, x = , y = ;
- scanf("%d%d", &n, &m);
- for(int i=; i<m; i++)
- {
- scanf("%d%d%lf", &u, &v, &w);
- add(u, v, w);
- y = max(y, w);
- }
- printf("Case #%d: ",++kase);
- if(!check(y+)) printf("No cycle found.\n");
- else
- {
- while(y - x > 1e-)
- {
- double mid = x + (y-x)/(double);
- if(check(mid)) y = mid;
- else x = mid;
- }
- printf("%.2lf\n",x);
- }
- }
- return ;
- }
Going in Cycle!! UVA - 11090(二分+判断环路 )的更多相关文章
- 在环中(Going in Cycle!!, UVa 11090)
[题目描述] 给定一个 n 个点 m 条边的加权有向图,求平均权值最小的回路. [输入格式] 输入第一行为数据组数 T .每组数据第一行为图的点数 n 和边数 m (n ≤ 50).以下 m 行每行3 ...
- 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)
layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...
- UVA - 11090 - Going in Cycle!!(二分+差分约束系统)
Problem UVA - 11090 - Going in Cycle!! Time Limit: 3000 mSec Problem Description You are given a we ...
- UVA 11090 - Going in Cycle!!(Bellman-Ford)
UVA 11090 - Going in Cycle!! option=com_onlinejudge&Itemid=8&page=show_problem&category= ...
- POJ_2318_TOYS&&POJ_2398_Toy Storage_二分+判断直线和点的位置关系
POJ_2318_TOYS&&POJ_2398_Toy Storage_二分+判断直线和点的位置 Description Calculate the number of toys th ...
- UVA 11090 Going in Cycle!! SPFA判断负环+二分
原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 11090 Going in Cycle!! 环平均权值(bellman-ford,spfa,二分)
题意: 给定一个n个点m条边的带权有向图,求平均权值最小的回路的平均权值? 思路: 首先,图中得有环的存在才有解,其次再解决这个最小平均权值为多少.一般这种就是二分猜平均权值了,因为环在哪也难以找出来 ...
- UVA 11090 Going in Cycle!!(二分答案+判负环)
在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...
- UVa 11090 Going in Cycle!!【Bellman_Ford】
题意:给出n个点m条边的加权有向图,求平均值最小的回路 自己想的是用DFS找环(真是too young),在比较找到各个环的平均权值,可是代码实现不了,觉得又不太对 后来看书= =好巧妙的办法, 使用 ...
随机推荐
- Q&As:1.cocos2d-html5如何获得鼠标划过事件
不喜欢按部就班学东西,感觉各种框架各种技术就应该是拿到手用的,这应该是导致我现在学了这么多却没一样精通的缘故吧. 发现自己喜欢在QQ群回答一些菜鸟的问题,就算自己不清楚也会乐意看代码帮助解决╮(╯_╰ ...
- Supervisor4.0和python2.7的crit问题,导致python进程阻塞
1.问题原因 Supervisor高版本在守护python2.7的服务时,会crit并报错并倒至进程阻塞(python进程存在,但不在运行)的问题,一般会和字符集有关系 <type 'excep ...
- Unity利用Share SDK实现QQ、微信及微博第三方登录及定制内容分享(附代码)
最近因为公司的项目需要添加一些实用性的功能,需要添加第三方登录及分享,采用的是Mob的SDK,可以先到其官网下载对应的SDK 点击这里,为了方便后期进行数据统计和分析,所以可以先添加一个应用,添加成功 ...
- 最短路径算法(I)
弗洛伊德算法(Floyed-Warshall) 适用范围及时间复杂度 该算法的时间复杂度为O(N^3),适用于出现负边权的情况. 可以求取最短路径或判断路径是否连通.可用于求最小环,比较两点之间的大小 ...
- 亚马逊:PS4和Xbox One实在太火
圣诞节刚刚结束,当实体零售商在抱怨坑爹的天气让自己节日生意变得冷清的同时,在线零售商们却依旧赚的盆满钵满. 亚马逊近日表示,今年节日期间的零售工作非常不错,新一代游戏机更是最大的亮点.据销售统计,在圣 ...
- oozie的shell-action中加入hive脚本命令启动执行shell同时操作hive,抛异常Container killed on request. Exit code is 143 Container exited with a non-zero exit code 143
使用oozie来调度操作,用shell的action执行命令,其中shell里包含着hive -e 操作执行时,oozie窗口报 WARN ShellActionExecutor: - SERVER[ ...
- centos上搭建git服务--2
在 Linux 下搭建 Git 服务器 环境: 服务器 CentOS6.6 + git(version 1.7.1)客户端 Windows10 + git(version 2.8.4.window ...
- npm 常用指令
npm install <name>安装nodejs的依赖包 例如npm install express 就会默认安装express的最新版本,也可以通过在后面加版本号的方式安装指定版本, ...
- Scrum立会报告+燃尽图(十月十二日总第三次):视频相关工作
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2190 Scrum立会master:孙赛佳 一.小组介绍 组长:付佳 组员 ...
- 20135208 JAVA第四次实验
课程:Java程序与设计 班级:1352 姓名:贺邦 小组成员: 20135212池彬宁 20135208贺邦 学号:20135208 成绩: 指导教师:娄嘉鹏 ...