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. mysql学习之通过文件创建数据库以及添加数据

    转自:http://blog.163.com/wujicaiguai@126/blog/static/170171558201411311547655/ 1.# 创建数据库语句 create data ...

  2. Win7系统出现提示: “Windows已遇到关键问题,将在一分钟后自动重新启动。”

    1. 若用户在使用Win7系统时,遇到上述系统故障,建议重启电脑.等电脑开机自检一过,马上按键盘上的F8键,选择进入安全模式.在安全模式下,进行系统还原.其他的解决方法见下. 1.或者,在安全模式下, ...

  3. OkHttp下载文件中途断网报Can't create handler inside thread that has not called Looper.prepare()异常的解决办法

    最近做项目时出现个问题. 在一个基类中,创建一个Handler对象用于主线程向子线程发送数据,代码如下: this.mThirdHandler = new Handler(){ @Override p ...

  4. 纯js手动分页

    昨天让做个页面,后台提供所有数据,没有做好分页,需要前端js手动分页. 我参考了 http://www.cnblogs.com/jiechn/p/4095029.html 做了些许改动让分页效果更加完 ...

  5. [已读]JavaScript面向对象编程指南

    又是一个忽悠人的书名,其实这本书的花了大量内容阐述JS的基础语法,BOM,DOM,事件,ajax(这个和很多js书一样).最后一章则是编程模式与设计模式. 我觉得与面向对象没多大关系,要算的话,pro ...

  6. AJPFX关于java中可访问控制符和非访问控制符的详细总结

    1.类的修饰符分为:可访问控制符和非访问控制符两种. 可访问控制符是:公共类修饰符 public 非访问控制符有:抽象类修饰符 abstract :最终类修饰符 final 1 )公共类修饰符 pub ...

  7. Failed to obtain lock on file /usr/local/nagios/var/ndo2db.lock: Permission denied : Permission denied

    Failed to obtain lock on file /usr/local/nagios/var/ndo2db.lock: Permission denied  : Permission den ...

  8. 解决jenkins插件列表为空的问题

    可能是由于Jenkins的更新网站被QIANG,因此,请使用 http://mirror.xmission.com/jenkins/updates/update-center.json 来进行更新

  9. 洛谷 大牛分站 P1000 超级玛丽游戏

    题目背景 本题是洛谷的试机题目,可以帮助了解洛谷的使用. 建议完成本题目后继续尝试P1001.P1008. 题目描述 超级玛丽是一个非常经典的游戏.请你用字符画的形式输出超级玛丽中的一个场景. *** ...

  10. 51nod 1031 骨牌覆盖

    基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 在2*N的一个长方形方格中,用一个1*2的骨牌排满方格.   问有多少种不同的排列方法.   例如: ...