zoukankan      html  css  js  c++  java
  • GYM 101572I(有向图上最小环)

    逗号空格是假的,全都直接连边就行。
    提供一个迪杰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);
    	}
    }
    
  • 相关阅读:
    xss框架(一)之浏览器通信
    Joomla未授权创建特权用户漏洞和getshell脚本解析
    从零开始写网站登录爆破(一)
    CSRF学习整理
    vue中vue2-google-maps使用谷歌地图的基础操作
    vue中百度地图API的调用
    60秒定时减少
    git操作指令,以及常规git代码操作
    taro taroUi的H5打包后路径/修改为./
    vue enter事件无效,加入native
  • 原文地址:https://www.cnblogs.com/AlphaWA/p/10918068.html
Copyright © 2011-2022 走看看