图论trainning-part-1 D. Going in Cycle!!
D. Going in Cycle!!
64-bit integer IO format: %lld Java class name: Main
- 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!!的更多相关文章
- 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 ...
- 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 ...
- AtCoder AGC036D Negative Cycle (图论、DP)
题目链接 https://atcoder.jp/contests/agc036/tasks/agc036_d 题解 这都是怎么想出来的啊..目瞪口呆系列.. 第一步转化至关重要: 一张图中不存在负环意 ...
- [CF580C]Shortest Cycle(图论,最小环)
Description: 给 \(n\) 个点的图,点有点权 \(a_i\) ,两点之间有边当且仅当 \(a_i\ \text{and}\ a_j \not= 0\),边权为1,求最小环. Solut ...
- SDUT OJ 数据结构实验之图论十:判断给定图是否存在合法拓扑序列
数据结构实验之图论十:判断给定图是否存在合法拓扑序列 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Prob ...
- 【杂题总汇】HDU-5215 Cycle
◆HDU-5215◆ Cycle 国庆节集训的第三天……讲图论,心情愉快……刷了一堆水题,不过也刷了一些有意思的题 +传送门+ HDU ▶ 题目 给出一个无向图(无自环,无重边),求该无向图中是否存在 ...
- Python小白的数学建模课-15.图论基本概念
图论中所说的图,不是图形图像或地图,而是指由顶点和边所构成的图形结构. 图论不仅与拓扑学.计算机数据结构和算法密切相关,而且正在成为机器学习的关键技术. 本系列结合数学建模的应用需求,来介绍 Netw ...
- 使用JSONObject.fromObject的时候出现“There is a cycle in the hierarchy”异常 的解决办法
在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常. 意思是出现了死循环,也就是Model之间有循环包含关系: ...
- JS案例之2——cycle元素轮播
元素轮播效果是页面中经常会使用的一种效果.这个例子实现了通过元素的隐藏和显示来表现轮播效果.效果比较简单. 效果图如下: 源代码如下: <!DOCTYPE html> <html&g ...
随机推荐
- 在Android 源码中添加系统服务
Android系统本身提供了很多系统服务,如WindowManagerService,PowerManagerService等.下面描述一下添加一个系统服务的具体步骤. 1.定义自定义系统服务接口 撰 ...
- bash 变量传递方法
###1.sh ##(该sh 目的是 将变量env传入env.sh, 同时让env.sh在当前事物生效,最后执行env.sh 定义的变量envs) export ENV=prepareecho ...
- SqlParameter 操作 image 字段
public static void AddEmployee( string lastName, string firstName, string title, DateTime hireDate, ...
- Vue 页面加载闪现代码问题
CSS中 [v-cloak] { display: none; } HTML中 <div v-cloak> {{ message }} </div> 显示代码主要是{{}}这个 ...
- MVC 下 ajax调用 日期差值计算
背景: 服务项目已有服务期起止时间From-To 现在要根据用户输入的新的起始时间, 和该服务期的原有区间值, 计算出新的服务期截止时间 即 NewServiceToDateTime = NewSer ...
- Android开发二维码之坑
之前一直做的是.NET开发用的是C#语言,近段时间由于做一个APP这才用上了java,在二维码扫描整合到APP里面遇到扫描二维码之后没有返回值,经过反复的尝试最后终于拿到了返回值,之后觉得很有必要记录 ...
- 关于bin和obj文件夹。debug 和release的区别(转)
关于bin和obj文件夹. 楼主hcaihao(影子男孩)2002-05-29 20:04:24 在 .NET技术 / C# 提问 VS.Net会生成bin和obj文件夹以及它们下面的Debug和Re ...
- 职业生涯手记——记人生中第一次经历的产品上线——内测篇Day11
2017/08/21 产品内测期Day11 说出来可能你不信,原定于9月15号结束的内测活动,今天居然被甲方投诉导致强制停止,原因是这个内测活动没有经过批准,并且有用户打了甲方所在公司的客服部门,增加 ...
- pylint安装失败的解决方法
原文链接http://www.cnblogs.com/Loonger/p/7815335.html 使用命令pip3 install pylint安装pylint是出现错误.查了一圈也找不到答案.仔细 ...
- Socket是什么呢?中间软件抽象层
代表着网络连接 Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面,对用 ...