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. DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph

    题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...

  2. 1-16使用try-catch捕捉异常

    处理异常 可以使用try-catch-处理异常,例如之前的程序可以使用try-catch-处理 package com.monkey1024.exception; import java.io.Fil ...

  3. 516 Longest Palindromic Subsequence 最长回文子序列

    给定一个字符串s,找到其中最长的回文子序列.可以假设s的最大长度为1000. 详见:https://leetcode.com/problems/longest-palindromic-subseque ...

  4. iOS面试题之runloop

    本文围绕以下几个部分展开对runloop的叙述. 1.runloop是什么/runloop的概念? 2.NSRunLoop 和 CFRunLoopRef? 3.runloop和线程的关系? 4.run ...

  5. position 位置、表单

    一.position    位置 1.只要使用了定位,必须有一个相对的参照物 2.具体定位的那个元素需加position:absolute:(绝对的)   绝对的:就是具体到某一个地方,特别详细的意思 ...

  6. RecyclerView 缓存机制学习笔记2

    RecyclerView 初始化所有的视图后,调用 去缓存(StaggeredGridLayoutManager), 而不是初始化一次缓存一次 存储后系统又会去调用tryGetViewHolderFo ...

  7. 联玛客(W 笔试)

    纸质算法题 1. 输入数据:1.3.2.4.8... 输出数据:3.1.4.2.8... 找出规律,写出一个程序求解,并附上时间复杂度和空间复杂度 我的答案: 规律一:奇偶位互换 假设输入数据长度为5 ...

  8. END - 提交当前的事务

    SYNOPSIS END [ WORK | TRANSACTION ] DESCRIPTION 描述 END END 提交当前事务. 所有当前事务做的修改都可被其它事务看到并且保证在发生崩溃的情况下的 ...

  9. python_使用qrcode生成二维码

    1.功能 使用qrcode生成二维码 2.代码 #生成二维码: import qrcode #根据url生成二维码 def qrcodeWithUrl(url): img = qrcode.make( ...

  10. uva820 Internet Bandwidth

    就是模板... #include<cstdio> #include<cstring> #include<vector> #include<queue> ...