题目链接: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. postman小白教程

    转载:之前看到的保存了下来,没有找到转载地址,所以如果侵权的话联系我加下转载地址,感觉这篇文章写的很好,详细

  2. python2.7打印中文乱码的问题解决

    一. import sys reload(sys) sys.setdefaultencoding('utf-8') print('测试中文') 二. print(‘我是中国人’) >>&g ...

  3. 实现atoi

    1. 去掉首位空格 2. 判断首位是否有正负号 3. 判断各位是否是0~9,有其他字符直接返回当前结果   public class Solution { public int atoi(String ...

  4. 来自python自学者的小问题

    我想使用python的第三方库,但是我的IDE给我一个错误代码: D:\untitled\venv\Scripts\python.exe "D:/py code/venv/sxsxsxsxs ...

  5. (136)leetcode刷题Python笔记——只出现一次的数字

    题目如下: 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: ...

  6. C#判断页面中的多个文本框输入值是否有重复的实现方法

    List<string> list = new List<string>();//首先定义一个泛型数组 //这里假如说有四个文本框 string mainseat = this ...

  7. C#将MD5后的字符串转为字符数据,随机大小写

    一如下代码 public static string GenerateCode(Guid id, DateTime endTime, string Type) { string str = id + ...

  8. Webpack和Gulp对比

    Webpack和Gulp对比 作者 彬_仔 关注 2016.10.19 22:42* 字数 8012 阅读 2471评论 18喜欢 68 在现在的前端开发中,前后端分离.模块化开发.版本控制.文件合并 ...

  9. Spark Thrift Server

    ThriftServer是一个JDBC/ODBC接口,用户可以通过JDBC/ODBC连接ThriftServer来访问SparkSQL的数据.ThriftServer在启动的时候,会启动了一个Spar ...

  10. zTree根节点默认打开

    1.在生成tree的json数据中,直接给出:open:true的属性; 2.treeObj.expandAll(true); 3.var zTree = $.fn.zTree.getZTreeObj ...