반응형
https://www.acmicpc.net/problem/1948
1948번: 임계경로
첫째 줄에 도시의 개수 n(1 ≤ n ≤ 10,000)이 주어지고 둘째 줄에는 도로의 개수 m(1 ≤ m ≤ 100,000)이 주어진다. 그리고 셋째 줄부터 m+2줄까지 다음과 같은 도로의 정보가 주어진다. 처음에는 도로의
www.acmicpc.net
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int,int> > adj[10001];
vector<pair<int, int> > rev_adj[10001];
int redegree[10001];
int rev_res[10001];
int degree[10001];
int res[10001];
bool vis[10001];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b,c;
cin >> a >> b >> c;
adj[a].push_back({ b,c });
rev_adj[b].push_back({ a,c });
redegree[a]++;
degree[b]++;
}
queue<int> Q;
int s, e;
cin >> s >> e;
Q.push(s);
while (!Q.empty()) {
int x = Q.front();
Q.pop();
for (auto i : adj[x]) {
int nx = i.first;
int nd = i.second;
res[nx] = max(res[nx], res[x] + nd);
if (--degree[nx] == 0) {
Q.push(nx);
}
}
}
int ans = res[e];
cout << res[e] << '\n';
Q.push(e);
while (!Q.empty()) {
int x = Q.front();
Q.pop();
for (auto i : rev_adj[x]) {
int nx = i.first;
int nd = i.second;
rev_res[nx] = max(rev_res[nx], rev_res[x] + nd);
if (--redegree[nx] == 0) {
Q.push(nx);
}
}
}
set<pair<int, int> > Set;
Q.push(s);
vis[s] = 1;
while (!Q.empty()) {
int x = Q.front(); Q.pop();
for (auto i : adj[x]) {
int nx = i.first;
int d = i.second;
if ((res[nx] == res[x] + d) && (res[nx] + rev_res[nx] == ans)) {
if (!vis[nx]) {
Q.push(nx);
vis[nx] = 1;
}
Set.insert({ x,nx });
}
}
}
cout << Set.size() << '\n';
return 0;
}

반응형
'Algorithm > problem' 카테고리의 다른 글
백준 1199번: 오일러 회로 시간초과 해결 (0) | 2022.02.28 |
---|---|
백준 18111번 : 마인크래프트 C++ 코드 (0) | 2022.02.26 |
백준 2848번 : 알고스팟어 - 위상정렬 C++ 코드 (0) | 2022.02.25 |
백준 2056번: 작업 - 위상정렬 dp C++ 코드 (0) | 2022.02.25 |
백준 21276번 : 계보 복원가 호석 - 위상정렬 C++ 코드 (0) | 2022.02.25 |