bzoj 1486: [HNOI2009]最小圈 dfs求负环
1486: [HNOI2009]最小圈
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 1022 Solved: 487
[Submit][Status]
Description


最开始写floyd求负环结果TLE了,改成dfs后速度变成原来的100+倍。反正还是比较神奇。
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- #define MAXN 4000
- #define MAXE 50000
- #define MAXV 10000
- #define PROB "loop"
- #define eps 1e-4
- #define INF 0x3f3f3f3f3f3f3f3fLL
- #define inf 1E1000
- #ifdef unix
- #define LL "%lld"
- #else
- #define LL "%I64d"
- #endif
- typedef long long qword;
- typedef long double real;
- struct edge
- {
- int x,y;
- real z;
- }el[MAXE];
- struct Edge
- {
- int np;
- real val;
- Edge *next;
- }E[MAXE],*V[MAXV];
- int tope=-;
- void addedge(int x,int y,real z)
- {
- E[++tope].np=y;
- E[tope].val=z;
- E[tope].next=V[x];
- V[x]=&E[tope];
- }
- bool state[MAXN];
- real dst[MAXN];
- bool dfs(int now)
- {
- Edge *ne;
- state[now]=true;
- for (ne=V[now];ne;ne=ne->next)
- {
- if (dst[ne->np]>dst[now]+ne->val)
- {
- if (state[ne->np])return true;
- dst[ne->np]=dst[now]+ne->val;
- if (dfs(ne->np))return true;
- }
- }
- state[now]=false;
- return false;
- }
- int main()
- {
- // freopen(PROB".in","r",stdin);
- //freopen(PROB".out","w",stdout);
- freopen("input.txt","r",stdin);
- int n,m;
- scanf("%d%d",&n,&m);
- int i,j,k,x,y;
- real z;
- real l,r,mid;
- l=r=;
- for (i=;i<m;i++)
- {
- scanf("%d%d%Lf",&x,&y,&z);
- x--;y--;
- addedge(x,y,z);
- // addedge(y,x,z);
- if(z<)l+=z;
- else r+=z;
- }
- bool flag;
- while (l+1e-<r)
- {
- mid=(l+r)/;
- for (i=;i<=n;i++)dst[i]=,state[i]=false;;
- flag=false;
- for (i=;i<=tope;i++)E[i].val-=mid;
- for (i=;i<=n;i++)
- {
- if (dfs(i))
- {
- flag=true;
- break;
- }
- }
- for(i=;i<=tope;i++)E[i].val+=mid;
- if (flag)
- {
- r=mid;
- }else
- {
- l=mid;
- }
- }
- printf("%.8Lf\n",(real)r);
- }
bzoj 1486: [HNOI2009]最小圈 dfs求负环的更多相关文章
- BZOJ 1486: [HNOI2009]最小圈( 二分答案 + dfs判负圈 )
二分答案m, 然后全部边权减掉m, 假如存在负圈, 那么说明有平均值更小的圈存在. 负圈用dfs判断. ------------------------------------------------ ...
- [HNOI2009]最小圈 (二分答案+负环)
题面:[HNOI2009]最小圈 题目描述: 考虑带权的有向图\(G=(V,E)\)以及\(w:E\rightarrow R\),每条边\(e=(i,j)(i\neq j,i\in V,j\in V) ...
- [BZOJ 1486][HNOI2009]最小圈(二分答案+dfs写的spfa判负环)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1486 分析:容易想到先二分答案x,然后把所有边的权值-x,那么如果图中存在权值和为0的 ...
- bzoj 1486: [HNOI2009]最小圈
Description Input Output Sample Input 4 5 1 2 5 2 3 5 3 1 5 2 4 3 4 1 3 Sample Output 3.66666667 HIN ...
- BZOJ 1486: [HNOI2009]最小圈 [01分数规划]
裸题...平均权值最小的环.... 注意$dfs-spfa$时$dfs(cl)$...不要写成$dfs(u)$ #include <iostream> #include <cstdi ...
- 1486: [HNOI2009]最小圈 - BZOJ
在机房的小伙伴提醒是二分之后,我想到了是判负环,所以我用spfa,而且我保持dis都是小于等于0,本以为这样就能过了,可是还是有一个点达到了3.8s左右(其他都是0.0几秒) 所以还是写了dfs版 ...
- 【BZOJ】1486 [HNOI2009]最小圈
[算法]二分+spfa [题解]据说这个叫分数规划? 0-1分数规划 二分答案a,则对于任意的环有w/k≤a即w-ak≤0,若满足条件则a变小,否则a变大. 因为w=w1+w2+...+wk,所以变形 ...
- 1486: [HNOI2009]最小圈
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 3129 Solved: 1543[Submit][Status][Discuss] Descripti ...
- bzoj千题计划227:bzoj1486: [HNOI2009]最小圈
http://www.lydsy.com/JudgeOnline/problem.php?id=1486 二分答案 dfs版spfa判负环 #include<queue> #include ...
随机推荐
- C# 如何设置 richTextBoxr的边距
附件 http://files.cnblogs.com/xe2011/richTextBox_EM_SETRECT.rar using System.Runtime.InteropServices; ...
- 【JavaScript设计模式系列---开篇预览】
转:http://www.cnblogs.com/Darren_code/archive/2011/08/31/JavascripDesignPatterns.html 2011-08-31 23:5 ...
- Sending e-mail with Spring MVC---reference
reference from:http://www.codejava.net/frameworks/spring/sending-e-mail-with-spring-mvc Table of con ...
- 在Nginx中搭建Nagios监控平台
本文只做Nginx下Nagiox安装的说明,其它关于Nagios监控的详细配置请参考我的另一篇文章[Ubuntu 10.04下构建Nagios监控平台] Nagios依赖PHP环境和perl环境.由于 ...
- RedHat7搭建无人值守自动安装Linux操作系统(PXE+Kickstart)
Kickstart服务器 IP: 192.168.136.253 掩码:255.255.255.0 网关:192.168.136.2 DNS:192.168.136.2 安装部署HTTP服 ...
- IE8下网页中的视频会遮挡住顶层DIV的解决办法
在IE8浏览器下,发现网页中的视频会遮挡住本来固定在最顶层的DIV.即便使用z-index也无法解决.但是其他浏览器是正常的. 解决的办法很简单,就是在调用flash视频播放器的时候,加上一个参数“o ...
- 爆牙齿的 Web 标准面试题 【转藏】
<!DOCTYPE html> <html lang="zh-CN"><head> <meta http-equiv="cont ...
- HTTP 错误 500.19- Internal Server Error 错误解决方法 分类: Windows服务器配置 2015-01-08 20:16 131人阅读 评论(0) 收藏
1.第一种情况如下: 解决方法如下: 经过检查发现是由于先安装Framework组件,后安装iis的缘故,只需重新注册下Framework就可以了,具体步骤如下 1 打开运行,输入cmd进入到命令提示 ...
- Android Studio SDK Manager无法正常下载如何设置
博客分类: Linux 零散小知识 Android那点事 AndroidStudioSDKManager 一方面在/etc/hosts中设置: #Google主页 203.208.46.146 ww ...
- rac中 kull session会话脚本
方法:ALTER SYSTEM KILL SESSION '80, 6, @2'; --<= 80 sid,6 serial#,@2 inst_id kill session 脚本如下:sel ...