题目链接: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的更多相关文章

  1. 《Pro AngularJS》学习小结-02

    上一篇的项目只有一个单独的模板页面,加入了相应的controller,filter,使得页面上的数据能够动态的变化.现在我们开始建立并整合多个模板,加入购物车模块和结账checkout模块. 一.在页 ...

  2. 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 ...

  3. Gym 100917J---Judgement(01背包+bitset)

    题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...

  4. Gym 100917J---dir -C(RMQ--ST)

    题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...

  5. ACM: Gym 100935B Weird Cryptography - 简单的字符串处理

    Weird Cryptography Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  6. ACM: Gym 100935F A Poet Computer - 字典树

    Gym 100935F A Poet Computer Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d &am ...

  7. CodeForces Gym 100500A A. Poetry Challenge DFS

    Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  8. [Gym]2008-2009 ACM-ICPC, NEERC, Moscow Subregional Contest

    比赛链接:http://codeforces.com/gym/100861 A模拟,注意两个特殊的缩写. #include <bits/stdc++.h> using namespace ...

  9. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

随机推荐

  1. 内网渗透 - 提权 - Windows

    MS提权 MS16- MS16- 提权框架 Sherlock 信息收集 ifconfig -a cat /etc/hosts arp -a route -n cat /proc/net/* ping扫 ...

  2. 四种pop模式介绍

    四种pop模式介绍 URL:http://www.hishop.com.cn/ecschool/jd/show_21195.html URL:https://zhidao.baidu.com/ques ...

  3. js 正则整理

    //严格验证身份证格式方法function idCardNo(value){ //验证身份证号方法 var area = { 11: "北京", 12: "天津" ...

  4. 20191105 《Spring5高级编程》笔记-第10章

    第10章 使用类型转换和格式化进行验证 在应用程序开发中,数据验证通常与转换和格式化一起被提及.因为数据源的格式很可能与应用程序中所使用的格式不同. 名词缩写: SPI(Service Provide ...

  5. MySQL的练习

    mysql登录:方法1:使用Command Line Client登录,缺点:不显示报错信息 方法2:使用cmd登录(cmd的常用:查看ip地址:ipcongfi-----定时关机:shutdown ...

  6. WOJ#3836 Sightseeing Trip

    描述 给定一张无向图,求图中一个至少包含 3 个点的环,环上的节点不重复,并且环上的边的长度之和最小.该问题称为无向图的最小环问题.在本题中,你需要输出最小环的方案,若最小环不唯一,输出任意一个均可. ...

  7. CocosCreator与Laya2.0区别

    1图集: Laya:直接拖拽res里面的图片,当生成图集后,会自动优先使用图集的 Cocos:应该先打图集,且图集里的图就是图集里的图,资源里的图就是资源里的.2者不同 addChild Laya:会 ...

  8. python学习第四十二天列表生成式用法及作用

    在操作列表或者元组的时候,对一系列的数据进行算法,比较整个数据加1,或翻倍,用传统的算法就很繁琐,列表给我们提供简便的方法 a=[i*i for i in rang(10)] a=[1,4,9,16, ...

  9. 如果存在这个表,则删除这个表的sql

    mysql: drop table if exists address_book sqlserver2005:IF EXISTS (SELECT * FROM sys.objects WHERE ob ...

  10. 快速禁止Chrome浏览器缓存

    在前端的开发中,最麻烦的莫过于浏览器的缓存,经常需要清理缓存文件,导致开发效率较低. 但Chrome可以一键禁止浏览器缓存,并且在后续的操作中,无论相同的资源请求多少次,都不会缓存到本地,一起来体验下 ...