ABC277 E - Crystal Switches(分层图)
Posted Harris-H
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ABC277 E - Crystal Switches(分层图)相关的知识,希望对你有一定的参考价值。
ABC277 E - Crystal Switches(分层图)
居然忘记分层图了,菜死了!
二层图,对每个有开关的连 e d g e ( i , n + i , 0 ) edge(i,n+i,0) edge(i,n+i,0),然后所有边权 ( u , v , 1 ) , ( u + n , v + n , 1 ) (u,v,1),(u+n,v+n,1) (u,v,1),(u+n,v+n,1)
因为边权是1,跑个01-bfs即可。用deque实现。
时间复杂度: O ( n + m ) O(n+m) O(n+m)
#include <iostream>
#include <utility>
#include <vector>
#include <deque>
using namespace std;
typedef pair<int, int> edge;
const int inf = 1e9;
int n, m, k;
vector<edge> G[400005];
int dist[400005];
int main(void)
cin >> n >> m >> k;
int u, v, a, s;
for(int i = 1; i <= m; i++)
cin >> u >> v >> a;
if(a) G[u].push_back(edge(v, 1)), G[v].push_back(edge(u, 1));
else G[n+u].push_back(edge(n+v, 1)), G[n+v].push_back(edge(n+u, 1));
for(int i = 1; i <= k; i++)
cin >> s;
G[s].push_back(edge(n+s, 0)), G[n+s].push_back(edge(s, 0));
deque<int> deq;
deq.push_back(1);
for(int i = 1; i <= 2*n; i++) dist[i] = inf;
dist[1] = 0;
while(deq.size())
int v = deq.front();
deq.pop_front();
for(int i = 0; i < (int)G[v].size(); i++)
int u = G[v][i].first, c = G[v][i].second;
if(dist[u] > dist[v] + c)
dist[u] = dist[v] + c;
if(c == 0) deq.push_front(u);
else deq.push_back(u);
int ans = min(dist[n], dist[2*n]);
if(ans == inf) ans = -1;
cout << ans << endl;
return 0;
以上是关于ABC277 E - Crystal Switches(分层图)的主要内容,如果未能解决你的问题,请参考以下文章
在 Crystal Reports 2016 中使用自定义 XSD
在 Android App Bundle 中添加 SwitchCompat 后,资源未找到错误 res/drawable/abc_switch_thumb_material.xml