博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图论trainning-part-1 D. Going in Cycle!!
阅读量:5337 次
发布时间:2019-06-15

本文共 2769 字,大约阅读时间需要 9 分钟。

 

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, 
N
N test cases follow. Each one starts with two numbers 
n and 
m
m 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负权回路判定

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #define LL long long11 #define INF 0x3f3f3ff12 using namespace std;13 const int maxn = 60;14 const double exps = 1e-3;15 struct arc {16 int to;17 double w;18 };19 20 vector
g[maxn];21 int cnt[maxn],n,m;22 double d[maxn];23 bool vis[maxn];24 bool spfa() {25 int i,j,v,u;26 queue
q;27 for(i = 1; i <= n; i++) {28 q.push(i);29 d[i] = INF;30 vis[i] = true;31 cnt[i] = 1;32 }33 d[1] = 0;34 while(!q.empty()) {35 u = q.front();36 q.pop();37 vis[u] = false;38 for(i = 0; i < g[u].size(); i++) {39 v = g[u][i].to;40 if(d[v] > d[u]+g[u][i].w) {41 d[v] = d[u]+g[u][i].w;42 if(!vis[v]) {43 vis[v] = true;44 cnt[v]++;45 q.push(v);46 if(cnt[u] > n) return true;47 48 }49 }50 }51 }52 return false;53 }54 void modify(double val) {55 for(int i = 1; i <= n; i++) {56 for(int j = 0; j < g[i].size(); j++) {57 g[i][j].w += val;58 }59 }60 }61 bool test(double val) {62 modify(-val);63 bool it = spfa();64 modify(val);65 return it;66 }67 int main() {68 int t,i,j,u,v,k = 1;69 double lt,rt,mid,w;70 scanf("%d",&t);71 while(t--) {72 scanf("%d%d",&n,&m);73 for(i = 0; i <= n; i++)74 g[i].clear();75 lt = INF;76 rt = 0;77 for(i = 0; i < m; i++) {78 scanf("%d%d%lf",&u,&v,&w);79 g[u].push_back((arc) {v,w});80 if(lt > w) lt = w;81 if(w > rt) rt = w;82 }83 printf("Case #%d: ",k++);84 if(test(rt+1.0)) {85 while(rt-lt > exps) {86 mid = lt+(rt-lt)/2;87 if(test(mid)) rt = mid;88 else lt = mid;89 }90 printf("%.2f\n",rt);91 } else printf("No cycle found.\n");92 }93 return 0;94 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/3868711.html

你可能感兴趣的文章
hbuilder调底层运用,多张图片上传
查看>>
较快的maven的settings.xml文件
查看>>
Git之初体验 持续更新
查看>>
随手练——HDU 5015 矩阵快速幂
查看>>
Maven之setting.xml配置文件详解
查看>>
SDK目录结构
查看>>
malloc() & free()
查看>>
HDU 2063 过山车
查看>>
高精度1--加法
查看>>
String比较
查看>>
Django之Models
查看>>
CSS 透明度级别 及 背景透明
查看>>
Linux 的 date 日期的使用
查看>>
PHP zip压缩文件及解压
查看>>
SOAP web service用AFNetWorking实现请求
查看>>
Java变量类型,实例变量 与局部变量 静态变量
查看>>
mysql操作命令梳理(4)-中文乱码问题
查看>>
Python环境搭建(安装、验证与卸载)
查看>>
一个.NET通用JSON解析/构建类的实现(c#)
查看>>
Windows Phone开发(5):室内装修 转:http://blog.csdn.net/tcjiaan/article/details/7269014
查看>>