Gym 100917F Find the Length
题目链接:http://codeforces.com/gym/100917/problem/F
------------------------------------------------------------------------------------------------
给出一个无向正权无自环图 要求对于每个点 经过它的最短"简单环"的长度
其中简单环的定义是 环上每条无向边都经过且仅经过一次
显然这个环至少经过三条边 三个点
我们或许会产生这样一种思路 对于每次询问 我们以该点$s$作为起点
先处理出到其余每点的最短路 然后枚举一条边 两端分别为$u\ v$
用$s$分别到$u\ v$的最短路再加上这条边的长度来更新结果
然而这样连样例都过不了 因为会产生重边
于是我们考虑把最短路径所经过的边都标记一下(多个可选的话随便标记一个)
然后枚举边的时候碰到标记过的边就直接跳过
不过这样还存在一种情况 就是 $u\ v$ 属于最短路径边所构成树上的除根节点外同一子树
这种情况也是会有重边的 所以这里再判断以下去除就好
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = , E = N * N * ;
int firste[N], nexte[E], v[E], w[E], flag[E];
int used[N], dist[N], fa[N];
int n, e = ;
void build(int x, int y, int z)
{
nexte[++e] = firste[x];
firste[x] = e;
v[e] = y;
w[e] = z;
}
int main()
{
scanf("%d", &n);
int x;
for(int i = ; i <= n; ++i)
for(int j = ; j <= n; ++j)
{
scanf("%d", &x);
if(i >= j)
continue;
if(x != -)
{
build(i, j, x);
build(j, i, x);
}
}
for(int i = ; i <= n; ++i)
{
int u = i;
for(int j = ; j <= n; ++j)
fa[j] = j;
memset(dist, 0x3f, sizeof dist);
dist[u] = ;
for(int t = ; t < n; ++t)
{
used[u] = i;
int mdist = 1e9, tu;
for(int p = firste[u]; p; p = nexte[p])
if(dist[v[p]] > dist[u] + w[p])
dist[v[p]] = dist[u] + w[p];
for(int j = ; j <= n; ++j)
if(used[j] != i && dist[j] < mdist)
{
tu = j;
mdist = dist[j];
}
for(int p = firste[tu]; p; p = nexte[p])
if(dist[tu] == dist[v[p]] + w[p])
{
flag[p] = flag[p ^ ] = i;
if(v[p] != i)
fa[tu] = fa[v[p]];
break;
}
u = tu;
}
int ans = 1e9;
for(int p = ; p < e; p +=)
if(flag[p] != i && fa[v[p]] != fa[v[p ^ ]])
ans = min(ans, dist[v[p]] + dist[v[p ^ ]] + w[p]);
printf("%d\n", ans < 1e9 ? ans : -);
}
return ;
}
Gym 100917F Find the Length的更多相关文章
- 《Pro AngularJS》学习小结-02
上一篇的项目只有一个单独的模板页面,加入了相应的controller,filter,使得页面上的数据能够动态的变化.现在我们开始建立并整合多个模板,加入购物车模块和结账checkout模块. 一.在页 ...
- ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力
Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS Memory Limit:65536KB 64bit IO Fo ...
- Gym 100917J---Judgement(01背包+bitset)
题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...
- Gym 100917J---dir -C(RMQ--ST)
题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...
- ACM: Gym 100935B Weird Cryptography - 简单的字符串处理
Weird Cryptography Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- ACM: Gym 100935F A Poet Computer - 字典树
Gym 100935F A Poet Computer Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d &am ...
- CodeForces Gym 100500A A. Poetry Challenge DFS
Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- [Gym]2008-2009 ACM-ICPC, NEERC, Moscow Subregional Contest
比赛链接:http://codeforces.com/gym/100861 A模拟,注意两个特殊的缩写. #include <bits/stdc++.h> using namespace ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
随机推荐
- CGI 环境变量
CGI 环境变量 环境变量 意义 SERVER_NAME CGI脚本运行时的主机名和IP地址. SERVER_SOFTWARE 你的服务器的类型如: CERN/3.0 或 NCSA/1.3. GATE ...
- DevOps的前世今生
From今日头条:https://www.toutiao.com/a6534660729453150723/?tt_from=weixin&utm_campaign=client_share& ...
- Linux的tail命令查看文件
小文件一般用cat 查看,但是如果文件内容过多,用cat就不合适了 可以用tail命令 # 默认显示文件最后十行 tail a.txt # 监视文件的尾部内容,默认十行, 可以-n 20显示20行 ...
- 000 (H5*) 常见代码
目录: 1:HTML 1:块级元素(block-level) 2:行内元素(inline-level) 3:行内块元素(inline-block) 4: img标签 5:表单元素 6:自定义列表 d ...
- web 前端2 html css一些小问题技巧
html css一些小问题技巧 1 对于儿子块float后,父亲块如果没内容就不见了,如何让父亲块依然跟随飘起了的儿子块撑起来呢?? 用到的属性after方法 公共方法作为继承即可. 1.1 方法 ...
- 简单易用的leetcode开发测试工具(npm)
描述 最近在用es6解leetcode,当问题比较复杂时,有可能修正了新的错误,却影响了前面的流程.要用通用的测试工具,却又有杀鸡用牛刀的感觉,所以就写了个简单易用的leetcode开发测试工具,分享 ...
- hdu 3333 离线线段树 + 思维/树状数组 /在线主席树
#include<iostream> #include<cstdio> #include<string> #include<cmath> #includ ...
- SpringBoot 集成MyBatis 中的@MapperScan注解
SpringBoot 集成MyBatis 中的@MapperScan注解 2018年08月17日 11:41:02 文火慢炖 阅读数:398更多 个人分类: 环境搭建 在SpringBoot中集成My ...
- Spark Streaming Transformations
map(func):对DStream中的所有的元素进行func转换生成新的DStream flatMap(func):和map方法类似,先对DStream中的元素进行func运算,然后压平,就是说,如 ...
- 安装运行谷歌开源的TensorFlow Object Detection API视频物体识别系统
Linux安装 参照官方文档:https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/inst ...