逗号空格是假的,全都直接连边就行。

提供一个迪杰n次的图上最小环板子。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std; const int maxn = 505;
const int inf = 0x3f3f3f3f;
typedef pair<int, int> pii;
int n;
string s[maxn];
string t;
int cnt;
map<string, int> id;
vector<int> adj[maxn];
int dis[maxn], pre[maxn], Ans[maxn];
int ans = inf, final;
int vis[maxn]; int dij(int st) {
memset(pre, 0, sizeof pre);
memset(dis, 0x3f, sizeof dis);
priority_queue<pii, vector<pii>, greater<pii> > Q;
for (int i : adj[st]) {
Q.push({1, i});
dis[i] = 1;
pre[i] = st;
}
while (!Q.empty()) {
int d = Q.top().first, u = Q.top().second;
Q.pop();
if (d > dis[u]) continue; for (int i : adj[u]) {
if (dis[i] > dis[u] + 1) {
dis[i] = dis[u] + 1;
pre[i] = u;
if (i == st) return dis[i];
Q.push({dis[i], i});
}
}
}
return dis[st];
} void print(int cur) {
if (vis[cur]) return;
vis[cur] = 1;
print(Ans[cur]);
cout << s[cur] << " ";
} int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s[i];
id[s[i]] = i;
}
for (int i = 1; i <= n; i++) {
cin >> t >> cnt;
int u = id[t];
for (int j = 0; j < cnt; j++) {
cin >> t;
getline(cin, t);
t.erase(0, 1);
string tmp = "";
for (int aa = 0; aa < t.length(); aa++) {
if (t[aa] != ',') {
tmp += t[aa];
} else {
int v = id[tmp];
adj[u].push_back(v);
aa++;
tmp = "";
}
}
if (tmp != "") {
int v = id[tmp];
adj[u].push_back(v);
}
}
}
for (int i = 1; i <= n; i++) {
int tmp = dij(i);
if (ans > tmp) {
ans = tmp;
final = i;
memcpy(Ans, pre, sizeof pre);
}
}
if (ans == inf) {
puts("SHIP IT");
} else {
print(final);
}
}

GYM 101572I(有向图上最小环)的更多相关文章

  1. HIT 2739 - The Chinese Postman Problem - [带权有向图上的中国邮路问题][最小费用最大流]

    题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2739 Time limit : 1 sec Memory limit : 64 M A Chinese ...

  2. 【Floyd算法】Gym - 101572I - Import Spaghetti

    题意:有向图最小环,输出方案. #include<cstdio> #include<iostream> #include<string> #include<a ...

  3. 非负权值有向图上的单源最短路径算法之Dijkstra算法

    问题的提法是:给定一个没有负权值的有向图和其中一个点src作为源点(source),求从点src到其余个点的最短路径及路径长度.求解该问题的算法一般为Dijkstra算法. 假设图顶点个数为n,则针对 ...

  4. Codeforces 183C(有向图上的环长度)

    因为公用一个系统所以大家求gcd:衡量各点之间的拓扑位置,如果到达同一点有不同的长度则取gcd. #include <cstdio> #include <cstring> #i ...

  5. 【做题记录】[NOI2008] 假面舞会—有向图上的环与最长链

    luogu 1477 [NOI2008] 假面舞会 容易发现: 如果图中没有环,那么面具种数一定是所有联通块内最长链之和,最少为 \(3\) . 如果有环,则面具种数一定是所有环的大小的最大公约数. ...

  6. poj1734 Sightseeing trip【最小环】

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:8588   Accepted:3224   ...

  7. 关于Floyd求解最小环的问题

    最近学习了floyd的奇妙用处,求解最小环,自己的领悟写在了纸上. 对于一个最小环,显然至少要包含三个点(此处不把两个点的回路称之为环) 从大体上考虑的话,一定有一个点与左右两侧的点是直接连接的(即不 ...

  8. 解析·NOIP·冷门 CLZ最小环

    赐予我力量,去改变我所能改变的;赐予我勇气,去接受我不能改变的;并赐予我智慧,去分辨这两者. -----安东尼达斯 NOIP的图论题中有一部分是跟图上的环有关的.蒟蒻的我在USACO上刷题时发现了一种 ...

  9. Codeforces Gym 100431A Achromatic Number 欧拉回路

    原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...

随机推荐

  1. PHP之面向对象PHP之面向对象(面向对象是什么)

    PHP之面向对象(面向对象是什么) 一.总结 一句话总结: 面向对象就是类:类都要 属性 和 方法 比如人:属性比如身高体重,方法比如吃饭喝水 面向对象中 ,方法即是函数 : 属性即是变量 ,只是面相 ...

  2. 分享知识-快乐自己:Hibernate 关联映射

    关联关系映射--概念: 关联关系是使用最多的一种关系,非常重要.在内存中反映为实体关系,映射到DB中为主外键关系. 实体间的关联,即对外键的维护.关联关系的发生,即对外键数据的改变. 外键:外面的主键 ...

  3. codeforces 658C C. Bear and Forgotten Tree 3(tree+乱搞)

    题目链接: C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes ...

  4. POJ3417Network(LCA+树上查分||树剖+线段树)

    Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has jus ...

  5. POJ1195Mobile phones (从二维树状数组到cdq分治)

    Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows ...

  6. BZOJ_4987_Tree_树形DP

    BZOJ_4987_Tree_树形DP Description 从前有棵树. 找出K个点A1,A2,…,Ak. 使得∑dis(AiAi+1),(1<=i<=K-1)最小. Input 第一 ...

  7. bzoj4555: 求和sum 快速傅立叶变换

    题目大意 给定\(S(n,m)\)表示第二类斯特林数,定义函数\(f(n)\) \[f(n) = \sum_{i=0}^n\sum_{j=0}^iS(i,j)*2^j*(j!)\] 给定正整数\(n, ...

  8. 简单两步快速实现shiro的配置和使用,包含登录验证、角色验证、权限验证以及shiro登录注销流程(基于spring的方式,使用maven构建)

    前言: shiro因为其简单.可靠.实现方便而成为现在最常用的安全框架,那么这篇文章除了会用简洁明了的方式讲一下基于spring的shiro详细配置和登录注销功能使用之外,也会根据惯例在文章最后总结一 ...

  9. C#自定义控件 类似于Linechart

    界面效果: 对外提供的属性设置 /// <summary> /// 背景色 /// </summary> public Color BackColor; /// <sum ...

  10. oracle针对中文排序

    在oracle 9i之前,对中文的排序,是默认按2进制编码来进行排序的. 9i时增加了几种新的选择: 按中文拼音进行排序:SCHINESE_PINYIN_M 按中文部首进行排序:SCHINESE_RA ...