题目链接: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. tbox新增stackless协程支持

    tbox之前提供的stackfull协程库,虽然切换效率已经非常高了,但是由于每个协程都需要维护一个独立的堆栈, 内存空间利用率不是很高,在并发量非常大的时候,内存使用量会相当大. 之前考虑过采用st ...

  2. 2019南京网络赛E:K Sum

    Description: 定义函数 \[ f _n (k) = \sum _{l _1 = 1} ^n \sum _{l _2 = 1} ^n \cdots \sum _{l _k = 1} ^n \ ...

  3. document.domain location.hostname location.host

    document.domain    location.hostname     location.host   :https://www.cnblogs.com/shd-study/p/103031 ...

  4. 键盘按键KeyCode大全

  5. Arcmap10.7连接oracle,但不装oracle客户端的配置

    环境:arcgis 10.7,oracle服务端12cR1.理论上其他版本方法一样 使用情况:一般开发人员不安装oracle服务端,甚至oracle客户端也不装,此时要用arcmap连oracle需要 ...

  6. wxpython中复选框的基本使用源码实例

    #coding=utf-8 import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self,None,-1, ...

  7. C# ListView添加DragDrop

    先建立好ListView,ImageList,然后编写一个比较类在就是添加DragDrop事件了具体实现看代码吧 public partial class Form1 : Form { public  ...

  8. AOP记录日志

    1.自定义注解 @Target(ElementType.METHOD) //注解放置的目标位置,METHOD是可注解在方法级别上 @Retention(RetentionPolicy.RUNTIME) ...

  9. Maven将jar包放入本地库

    转自:https://blog.csdn.net/qq_33314107/article/details/73549256 这是由于Oracle授权问题,Maven3不提供Oracle JDBC dr ...

  10. 实现 unity MonoBehaviour API5.4 的消息

      顺序(第一次执行.忽略循环) 方法 说明 Editor 1 void Reset() 重置为默认值 ------------------------------------------------ ...