Gym 100917F Find the Length

题目链接:http://codeforces.com/gym/100917/problem/F

------------------------------------------------------------------------------------------------

给出一个无向正权无自环图 要求对于每个点 经过它的最短"简单环"的长度

其中简单环的定义是 环上每条无向边都经过且仅经过一次

显然这个环至少经过三条边 三个点

我们或许会产生这样一种思路 对于每次询问 我们以该点$s$作为起点

先处理出到其余每点的最短路 然后枚举一条边  两端分别为$u\ v$

用$s$分别到$u\ v$的最短路再加上这条边的长度来更新结果

然而这样连样例都过不了 因为会产生重边

于是我们考虑把最短路径所经过的边都标记一下(多个可选的话随便标记一个)

然后枚举边的时候碰到标记过的边就直接跳过

不过这样还存在一种情况 就是 $u\ v$ 属于最短路径边所构成树上的除根节点外同一子树

这种情况也是会有重边的 所以这里再判断以下去除就好

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <algorithm>
 5 using namespace std;
 6 const int N = 310, E = N * N * 2;
 7 int firste[N], nexte[E], v[E], w[E], flag[E];
 8 int used[N], dist[N], fa[N];
 9 int n, e = 1;
10 void build(int x, int y, int z)
11 {
12     nexte[++e] = firste[x];
13     firste[x] = e;
14     v[e] = y;
15     w[e] = z;
16 }
17 int main()
18 {
19     scanf("%d", &n);
20     int x;
21     for(int i = 1; i <= n; ++i)
22         for(int j = 1; j <= n; ++j)
23         {
24             scanf("%d", &x);
25             if(i >= j)
26                 continue;
27             if(x != -1)
28             {
29                 build(i, j, x);
30                 build(j, i, x);
31             }
32         }
33     for(int i = 1; i <= n; ++i)
34     {
35         int u = i;
36         for(int j = 1; j <= n; ++j)
37             fa[j] = j;
38         memset(dist, 0x3f, sizeof dist);
39         dist[u] = 0;
40         for(int t = 1; t < n; ++t)
41         {
42             used[u] = i;
43             int mdist = 1e9, tu;
44             for(int p = firste[u]; p; p = nexte[p])
45                 if(dist[v[p]] > dist[u] + w[p])
46                     dist[v[p]] = dist[u] + w[p];
47             for(int j = 1; j <= n; ++j)
48                 if(used[j] != i && dist[j] < mdist)
49                 {
50                     tu = j;
51                     mdist = dist[j];
52                 }
53             for(int p = firste[tu]; p; p = nexte[p])
54                 if(dist[tu] == dist[v[p]] + w[p])
55                 {
56                     flag[p] = flag[p ^ 1] = i;
57                     if(v[p] != i)
58                         fa[tu] = fa[v[p]];
59                     break;
60                 }
61             u = tu;
62         }
63         int ans = 1e9;
64         for(int p = 2; p < e; p +=2)
65             if(flag[p] != i && fa[v[p]] != fa[v[p ^ 1]])
66                 ans = min(ans, dist[v[p]] + dist[v[p ^ 1]] + w[p]);
67         printf("%d\n", ans < 1e9 ? ans : -1);
68     }
69     return 0;
70 }
时间: 2024-10-19 17:32:21

Gym 100917F Find the Length的相关文章

CodeForces Gym 100935D Enormous Carpet 快速幂取模

Enormous Carpet Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935D Description standard input/outputStatements Ameer is an upcoming and pretty talented problem solver who loves to solve problems using computers.

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

Weird Cryptography Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935B Description standard input/output Khaled was sitting in the garden under an apple tree, suddenly! , well... you should guess what happened, an

Codeforces Gym 100286J Javanese Cryptoanalysis 傻逼暴力

原题地址:http://codeforces.com/gym/100286/attachments/download/2013/20082009-acmicpc-northeastern-european-regional-contest-neerc-08-en.pdf 此题题意是给你一个单对单密文,让你还原为原文,原文有个性质是,每个单词都是元音和辅音交替组成. 做法是直接5重for,暴力枚举AEIOU分别对应的字母,然后检查,然后输出 详见代码: //#include<iostream>

Codeforces Gym 100570 E. Palindrome Query Manacher

E. Palindrome QueryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100570/problem/E Description De Prezer loves palindrome strings. A string s1s2...sn is palindrome if and only if it is equal to its reverse. De Prezer also love

codeforces Gym 100187L L. Ministry of Truth 水题

L. Ministry of Truth Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/K Description Andrey works in the Ministry of Truth. His work is changing articles in newspapers and magazines so that they praise the Party an

codeforces Gym 100187B B. A Lot of Joy

B. A Lot of Joy Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/A Description Two boys Gena and Petya wrote on two strips of paper the same string s that consisted of lowercase Latin letters. Then each boy took o

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

题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT manager Linus Gates announced his next proprietary open-source system "Winux 10.04 LTS" In this system command "dir -C" prints list of all

ACM: Gym 101047M Removing coins in Kem Kadr&#227;n - 暴力

Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Practice Description standard input/output Andréh and his friend Andréas are board-game aficionados. They know many of their friends

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

Gym 100935F A Poet Computer Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description standard input/output The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems. One of th