UVA 11090 SPFA+二分答案 解题报告
来源:互联网 发布:文本情感分析算法 编辑:程序博客网 时间:2024/05/16 17:55
让我看到你们的双手
Going in Cycle!!
Description
You are given a weighted directed graph with n vertices and m edges. Each cycle in the graph has aweight, which equals to sum of its edges. There are so many cycles in the graph with different weights.
In this problem we want to nd a cycle with the minimum mean.
Input
The rst line of input gives the number of cases, N. N test cases follow. Each one starts with twonumbers n and m. m lines follow, each has three positive number a; b; c which means there is an edgefrom vertex a to b with weight of c.
Output
For each test case output one line containing Case #x: followed by a number that is the lowest meancycle in graph with 2 digits after decimal place, if there is a cycle. Otherwise print No cycle found..
Constraints
• n <= 50
• a; b <= n
• c <= 10000000
Sample Input
2
2 1
1 2 1
2 2
1 2 2
2 1 3
Sample Output
Case #1: No cycle found.
Case #2: 2.50
【解题报告】
题目大意是给定一个有向图,求平均值最小的回路。
二分答案w,然后每条边都减去w判断是否有负环。
(有谁能告诉我为什么我的代码那么慢。。。)
代码如下:
#include<cstdio>#include<cstring>#include<algorithm>#include<deque>using namespace std;#define N 60#define inf 0x3f3f3f3f#define eps 1e-3int head[N],vis[N],cnt_e[N];double dis[N];int t,n,m,cnt;struct Edge{ int to,nxt; double w;}e[N*N];void adde(int u,int v,double w){ e[++cnt].w=w; e[cnt].to=v; e[cnt].nxt=head[u]; head[u]=cnt;}bool SPFA(int s){ for(int i=0;i<=n;++i) dis[i]=inf*1.0; memset(vis,0,sizeof(vis)); memset(cnt_e,0,sizeof(cnt_e)); deque<int> q; dis[s]=0,vis[s]=1; q.push_back(s); while(!q.empty()) { int now=q.front();q.pop_front(); vis[now]=0; for(int i=head[now];~i;i=e[i].nxt) { int v=e[i].to; if(dis[v]>dis[now]+e[i].w) { dis[v]=dis[now]+e[i].w; if(!vis[v]) { vis[v]=1; if(!q.empty()) { if(dis[v]>dis[q.front()]) q.push_back(v); else q.push_front(v); } else q.push_back(v); if(++cnt_e[v]>=n) return 0; } } } } return 1;}bool check(double x){ bool flag=0; for(int i=1;i<=n;++i) for(int j=head[i];~j;j=e[j].nxt) e[j].w-=x; for(int i=1;i<=n;++i) if(!SPFA(i)) flag=1; for(int i=1;i<=n;++i) for(int j=head[i];~j;j=e[j].nxt) e[j].w+=x; return flag;}int main(){ int cas=0; for(scanf("%d",&t);t;--t) { printf("Case #%d: ",++cas); double l=inf*1.0,r=0.0,mid=0.00; memset(head,-1,sizeof(head)); cnt=0; scanf("%d%d",&n,&m); for(int i=1;i<=m;++i) { int u,v;double w; scanf("%d%d%lf",&u,&v,&w); adde(u,v,w); l=min(l,w); r=max(r,w); } if(!check(r+1)) {printf("No cycle found.\n");continue;} while(r-l>eps) { mid=(r+l)/2.0; check(mid)?r=mid:l=mid; } printf("%.2lf\n",r); } return 0;}
- UVA 11090 SPFA+二分答案 解题报告
- CodeVS 1183 SPFA+二分答案 解题报告
- UVA11090 Going in Cycle!! 解题报告【判负环】【SPFA】【二分答案】
- UVA 11478 查分约束+二分答案 解题报告
- UVA11478 Halum 解题报告【图论】【二分答案】【SPFA】【差分约束系统】
- CodeVS1183 泥泞的道路 解题报告【二分答案+SPFA最长路】
- BZOJ 1052 二分答案 解题报告
- BZOJ4952 [Wf 2017] 二分答案 解题报告
- Uva 12124 Assemble 解题报告(二分)
- UVa 1422 Processor 解题报告(二分)
- UVa 11627 Slalom 解题报告(二分)
- Going in Cycle!!+二分答案+spfa判负环+裸题+uva
- NOIP2015 跳石头 解题报告(二分答案)
- BZOJ 4443(SCOI2015)网络流+二分答案 解题报告
- SPOJ 287 网络流最大流+二分答案 解题报告
- BZOJ 2792 [POI 2012] 二分答案 解题报告
- BZOJ 1822 计算几何+网络流+二分答案 解题报告
- POJ2318 TOYS 解题报告【二分答案+向量叉积】
- 解决php中Cannot send session cache limiter 的问题的方法
- Collection-ArrayList-Transient及序列化
- MYSQL从零开始学习笔记
- windows 查看端口被占用并杀死进程 pid
- WEB前端性能优化集合
- UVA 11090 SPFA+二分答案 解题报告
- Spark SQL 支持的Hive内置函数
- BASE64加密源码(JavaScript)
- 抄来的文章
- 反转排序算法(Reverse Sort)
- MTK Android Driver:memory
- Scrapy 安装错误处理!
- php.ini中error_reporting参数的配置
- resteasy