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. selenium处理的操作

  2. 166 Fraction to Recurring Decimal 分数到小数

    给定两个整数,分别表示分数的分子和分母,返回字符串格式的小数.如果小数部分为循环小数,则将重复部分括在括号内.例如,    给出 分子 = 1, 分母 = 2,返回 "0.5".  ...

  3. c8051单片机注意事项:

    一定要注意交叉开关问题:外设要想正确分配到指定引脚,一定要用配置工具确定分配到指定引脚:如果手动分配一定要仔细验证.这方面有个深刻的教训. 有个项目用c8051f020,用到2个串口,硬件已经确定好了 ...

  4. C. Bear and Colors 区间枚举的技巧

    http://codeforces.com/problemset/problem/673/C 先说一个枚举区间的技巧,枚举前缀,不要枚举后缀. 就是下面这个代码是不好的 ; i <= n; ++ ...

  5. CF782B The Meeting Place Cannot Be Changed

    题意: The main road in Bytecity is a straight line from south to north. Conveniently, there are coordi ...

  6. 【学习笔记】Base64编码解码原理及手动实现(C#)

    1.[Base64编码原理]@叶落为重生 -base64的编码都是按字符串长度,以每3个8bit的字符为一组,-然后针对每组,首先获取每个字符的ASCII编码,-然后将ASCII编码转换成8bit的二 ...

  7. javajsp,Servlet:Property 'Id' not found

    avax.el.PropertyNotFoundException: Property 'Id' not found on type  org.androidpn.server.model.CarSo ...

  8. git 配置免密上传,配置ssh key

    1.windows 打开git bash 控制台,linux 直接打开命令控制台,输入 ssh-keygen 一直enter 下一步 2.生成的文件windows 存放在c://users 路径下,l ...

  9. 【HEVC帧间预测论文】P1.2 An Efficient Inter Mode Decision Approach for H.264 Video Codin

    参考:An Efficient Inter Mode Decision Approach for H.264 Video Coding <HEVC标准介绍.HEVC帧间预测论文笔记>系列博 ...

  10. vim下ctrl + s 僵死问题的解决

    vim下ctrl + s 僵死问题的解决 vim  使用vim习惯性手残Ctrl+S ,解决方法 : Ctrl + Q 就能恢复了