D. Going in Cycle!!

Time Limit: 3000ms
Memory Limit: 131072KB

64-bit integer IO format: %lld      Java class name: Main

 
You are given a weighted directed graph with n vertices and m edges. Each cycle in the graph has a weight, which equals to sum of its edges. There are so many cycles in the graph with different weights. In this problem we want to find a cycle with the minimum mean.
 
Input 
The first line of input gives the number of cases, NN test cases follow. Each one starts with two numbers n and mm lines follow, each has three positive number a, b, c which means there is an edge from 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 mean cycle in graph with 2 digits after decimal place, if there is a cycle. Otherwise print No cycle found..
 

- n ≤ 50

- a, b ≤ n

- c ≤ 10000000

Sample Input

2
2 1
1 2 1
2 2
1 2 2
2 1 3

Output for Sample Input

Case #1: No cycle found.

Case #2: 2.50

解题:二分+spfa负权回路判定

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#include <queue>
#define LL long long
#define INF 0x3f3f3ff
using namespace std;
const int maxn = ;
const double exps = 1e-;
struct arc {
int to;
double w;
}; vector<arc>g[maxn];
int cnt[maxn],n,m;
double d[maxn];
bool vis[maxn];
bool spfa() {
int i,j,v,u;
queue<int>q;
for(i = ; i <= n; i++) {
q.push(i);
d[i] = INF;
vis[i] = true;
cnt[i] = ;
}
d[] = ;
while(!q.empty()) {
u = q.front();
q.pop();
vis[u] = false;
for(i = ; i < g[u].size(); i++) {
v = g[u][i].to;
if(d[v] > d[u]+g[u][i].w) {
d[v] = d[u]+g[u][i].w;
if(!vis[v]) {
vis[v] = true;
cnt[v]++;
q.push(v);
if(cnt[u] > n) return true; }
}
}
}
return false;
}
void modify(double val) {
for(int i = ; i <= n; i++) {
for(int j = ; j < g[i].size(); j++) {
g[i][j].w += val;
}
}
}
bool test(double val) {
modify(-val);
bool it = spfa();
modify(val);
return it;
}
int main() {
int t,i,j,u,v,k = ;
double lt,rt,mid,w;
scanf("%d",&t);
while(t--) {
scanf("%d%d",&n,&m);
for(i = ; i <= n; i++)
g[i].clear();
lt = INF;
rt = ;
for(i = ; i < m; i++) {
scanf("%d%d%lf",&u,&v,&w);
g[u].push_back((arc) {v,w});
if(lt > w) lt = w;
if(w > rt) rt = w;
}
printf("Case #%d: ",k++);
if(test(rt+1.0)) {
while(rt-lt > exps) {
mid = lt+(rt-lt)/;
if(test(mid)) rt = mid;
else lt = mid;
}
printf("%.2f\n",rt);
} else printf("No cycle found.\n");
}
return ;
}

图论trainning-part-1 D. Going in Cycle!!的更多相关文章

  1. Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 图论

    D. Vitaly and Cycle Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/p ...

  2. Codeforce 263D Cycle in Graph 搜索 图论 哈密尔顿环

    You've got a undirected graph G, consisting of n nodes. We will consider the nodes of the graph inde ...

  3. AtCoder AGC036D Negative Cycle (图论、DP)

    题目链接 https://atcoder.jp/contests/agc036/tasks/agc036_d 题解 这都是怎么想出来的啊..目瞪口呆系列.. 第一步转化至关重要: 一张图中不存在负环意 ...

  4. [CF580C]Shortest Cycle(图论,最小环)

    Description: 给 \(n\) 个点的图,点有点权 \(a_i\) ,两点之间有边当且仅当 \(a_i\ \text{and}\ a_j \not= 0\),边权为1,求最小环. Solut ...

  5. SDUT OJ 数据结构实验之图论十:判断给定图是否存在合法拓扑序列

    数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Prob ...

  6. 【杂题总汇】HDU-5215 Cycle

    ◆HDU-5215◆ Cycle 国庆节集训的第三天……讲图论,心情愉快……刷了一堆水题,不过也刷了一些有意思的题 +传送门+ HDU ▶ 题目 给出一个无向图(无自环,无重边),求该无向图中是否存在 ...

  7. Python小白的数学建模课-15.图论基本概念

    图论中所说的图,不是图形图像或地图,而是指由顶点和边所构成的图形结构. 图论不仅与拓扑学.计算机数据结构和算法密切相关,而且正在成为机器学习的关键技术. 本系列结合数学建模的应用需求,来介绍 Netw ...

  8. 使用JSONObject.fromObject的时候出现“There is a cycle in the hierarchy”异常 的解决办法

    在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常.   意思是出现了死循环,也就是Model之间有循环包含关系: ...

  9. JS案例之2——cycle元素轮播

    元素轮播效果是页面中经常会使用的一种效果.这个例子实现了通过元素的隐藏和显示来表现轮播效果.效果比较简单. 效果图如下: 源代码如下: <!DOCTYPE html> <html&g ...

随机推荐

  1. linux中用户组和用户

    linux中用户组和用户 1.介绍 在我们的linux系统,有很多用户组,也可以手动创建用户组,不同的用户组下面有很多的用户. 2.创建用户组及有关的命令 groupadd phpzu:创建一个php ...

  2. python_13(前端—cs)

    第1章 前端三大标准:1.1 三大标准介绍 1.2 html标签一览表 第2章 结构标准 html 2.1 html结构 2.2 html常用标签 2.2.1 h1-h6 2.2.2 span 2.2 ...

  3. Apache Kylin Cube 的存储

    不多说,直接上干货! 简单的说Cuboid的维度会映射为HBase的Rowkey,Cuboid的指标会映射为HBase的Value. Cube映射成HBase存储 如上图原始表所示:Hive表有两个维 ...

  4. 机器学习概念之特征选择(Feature selection)之VectorSlicer算法介绍

    不多说,直接上干货! VectorSlicer 算法介绍: VectorSlicer是一个转换器,输入特征向量,输出原始特征向量子集.VectorSlicer接收带有特定索引的向量列,通过对这些索引的 ...

  5. js去掉数组的空字符串

    后台返回数据的时候,有些数据为空时,一般都不进行显示,需要去除空字符串. 基本思路:获取数组张度,遍历数组,当数组某个值等于‘’或null或数据类型为undefined时,根据splice方法去除数据 ...

  6. Java关键字-volatile

    关键字volatile可以说是Java虚拟机提供的最轻量级的同步机制. 一旦某个共享变量(类的成员变量.类的静态成员变量)被volatile修饰之后,那么就具备了两层语义: 1.保证了不同线程对这个变 ...

  7. [ CQOI 2018 ] 异或序列

    \(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个不为空的子段异或和为 \(k\ ...

  8. JS正则表达式匹配<div><style>标签

    测试字符串: <style>v\:* {                 BEHAVIOR: url(#default#VML) } o\:* {                 BEHA ...

  9. 深入解析Web Services

    SOA,面向服务器建构,是一款架构,这几年虽然没前几年那么流行,但是还是有很多企业在用,而Web Services是目前适合做SOA的主要技术之一,通过使用Web Services,应用程序可以对外发 ...

  10. nvm、npm、nodejs的关系(转载)

    nvm.npm.nodejs的关系 为什么要了解nvm.npm.nodejs的关系: reactNative的项目构建都是有这几个工具进行构建管理. 掌握他们的关系,就能了解reactNative项目 ...