layout: post

title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)

author: "luowentaoaa"

catalog: true

mathjax: true

tags:

- 最短路

- 基础DP

- BellmanFord

- 图论

- 训练指南


Going in Cycle!!

UVA - 11090

题意

就最小的环的平均权值

题解

分枚举平均值mid,只需判断是否存在平均值小于mid的回路,即判断是否有sum(wi)<mid*k (1≤i≤k),只需判断是否有sum(wi-mid)<0,只需将边权值减去mid后,判断是否存在负环。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1e3+10;
const int inf=1000000000;
struct Edge
{
int from, to;
double dist;
Edge() {}
Edge(int u, int v, double d) : from(u), to(v), dist(d) {}
};
struct BellmanFord{
int n,m;
vector<Edge>edges;
vector<int> G[maxn];
bool inq[maxn]; /// 是否在队列中
double d[maxn]; /// s到各个点的距离 double 要改成double类型
int p[maxn]; /// 最短路中的上一条弧
int cnt[maxn]; /// 进队次数
void init(int n){
this->n=n;
for(int i=0;i<n;i++)G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, int dist)
{
edges.emplace_back(from, to, dist);
m = edges.size();
G[from].push_back(m - 1);
}
bool bellmanford(int s){
queue<int>Q;
memset(inq,0,sizeof(inq));
memset(cnt,0,sizeof(cnt));
for(int i = 0; i < n; i++) { d[i] = 0; inq[0] = true; Q.push(i); } //如果只判负环用这个
/*for(int i=0;i<n;i++)d[i]=inf;
d[s]=0;inq[s]=true;Q.push(s);*/
while(!Q.empty()){
int u=Q.front();
Q.pop();
inq[u]=false;
for(auto& id:G[u]){
Edge& e=edges[id];
if(d[u]<inf && d[e.to]>d[u]+e.dist){
d[e.to]=d[u] + e.dist;
p[e.to]=id;
if(!inq[e.to]){
Q.push(e.to);
inq[e.to]=true;
if(++cnt[e.to]>n)return true;
}
}
}
}
return false;
}
};
BellmanFord solver;
bool test(double x){
for(int i=0;i<solver.m;i++)
solver.edges[i].dist-=x;
bool ret=solver.bellmanford(0);
for(int i=0;i<solver.m;i++)
solver.edges[i].dist+=x;
return ret;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int t;
scanf("%d",&t);
for(int kase=1;kase<=t;kase++){
int n,m;
scanf("%d%d",&n,&m);
solver.init(n);
int ub=0;
while(m--){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);u--;v--;ub=max(ub,w);
solver.AddEdge(u,v,w);
}
printf("Case #%d: ", kase);
if(!test(ub+1))printf("No cycle found.\n");
else{
double l=0,r=ub;
while(r-l>1e-4){
double mid=l+(r-l)/2;
if(test(mid))r=mid;
else l=mid;
}
printf("%.2f\n",l);
}
}
return 0;
}

训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)的更多相关文章

  1. UVA11090 Going in Cycle!!(二分判负环)

    UVA11090 Going in Cycle!! 二分答案,用spfa判负环. 注意格式:图不一定连通. 复杂度$O(nmlog(maxw-minw))$ #include<iostream& ...

  2. BZOJ3597 [Scoi2014]方伯伯运椰子 【二分 + 判负环】

    题目链接 BZOJ3597 题解 orz一眼过去一点思路都没有 既然是流量网络,就要借鉴网络流的思想了 我们先处理一下那个比值,显然是一个分数规划,我们二分一个\(\lambda = \frac{X ...

  3. UVA 11090 Going in Cycle!! SPFA判断负环+二分

    原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  4. POJ-3259(最短路+Bellman-Ford算法判负圈)

    Wormholes POJ-3259 这题是最短路问题中判断是否存在负圈的模板题. 判断负圈的一个关键就是理解:如果在图中不存在从s可达的负圈,最短路径不会经过一个顶点两次.while循环最多执行v- ...

  5. Bellman-Ford算法判负环

    算法思想:如果没有负权回路,dis数组应该会在n-1次松弛之后结束. 算法复杂度:O(n*m).比Dijkstra算法复杂度要高. 代码: bool Bellman_Ford(int s) { int ...

  6. UVA11090 Going in Cycle (二分+判负环)

    二分法+spfa判负环.如果存在一个环sum(wi)<k*x,i=0,1,2...,k,那么每条边减去x以后会形成负环.因此可用spfa来判负环. 一般spfa判负环dfs最快,用stack次之 ...

  7. 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)

    layout: post title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束) author: "luowentaoaa" catal ...

  8. 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)

    layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...

  9. 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)

    layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...

随机推荐

  1. Codeforces Round #268 (Div. 1) 468D Tree(杜教题+树的重心+线段树+set)

    题目大意 给出一棵树,边上有权值,要求给出一个1到n的排列p,使得sigma d(i, pi)最大,且p的字典序尽量小. d(u, v)为树上两点u和v的距离 题解:一开始没看出来p需要每个数都不同, ...

  2. POJ3623 Best Cow Line, Gold 【后缀数组】

    最好的牛线,金 时间限制: 5000MS   内存限制: 65536K 提交总数: 5917   接受: 2048 描述 FJ即将把他的ñ(1≤ ñ ≤30,000)头牛竞争一年一度的"年度 ...

  3. rest与restful

      知乎上面摘抄的,感觉不错,分享下:  https://www.zhihu.com/question/28557115 1. REST描述的是在网络中client和server的一种交互形式:RES ...

  4. HBase并行写机制(mvcc)

    HBase在保证高性能的同时,为用户提供了便于理解的一致性数据模型MVCC (Multiversion Concurrency Control),即多版本并发控制技术,把数据库的行锁与行的多个版本结合 ...

  5. git上传本地项目

    1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库 git init 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点 ...

  6. iOS 快速框架搭建项目

    项目目录 Pod第三方 gitHub地址 https://github.com/henusjj/Basic-project-framework

  7. 河南省第十届省赛 Intelligent Parking Building

    title: Intelligent Parking Building 河南省第十届省赛 tags: [模拟,省赛] 题目描述: There is a new revolution in the pa ...

  8. mongoDB的文档查询

    1.简单查询: find() 方法以非结构化的方式来显示所有文档. 语法 MongoDB 查询数据的语法格式如下:      collection是集合名字,注意应该是当前数据库的集合,collect ...

  9. JS形参与实参问题

    JavaScript的参数传递也都是采用值传递的方式进行传值. (1)     通过实参调用函数的时候,传入函数里的是实参的副本而不是实参,因此在函数里面修改参数值并不会对实参造成影响. 例如:将全局 ...

  10. [转]树莓派gpio口控制

    0.前言     树莓派现在越来越火,网上树莓派的资料也越来越多.树莓派源自英国,国外嵌入式开源领域具有良好的分享精神,树莓派各种集成库也层出不穷,下面推荐几个. [[开发语言]——python [[ ...