Pagini recente » Cod sursa (job #2955435) | Cod sursa (job #2282037) | Cod sursa (job #2176819) | Cod sursa (job #2202590) | Cod sursa (job #3271540)
#include<bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
void drum_critic() {
int n, m;
vector<vector<int>> adj(100005);
vector<int> d(2005,0);
vector<int> t(2005,0);
vector<pair<int,int>> ans(2005,{0,0});
vector<int> p(2005,-1);
int timp = 0;
fin >> n;
for (int i = 1 ; i <= n ; i ++) {
fin >> t[i];
}
fin >> m;
for (int i = 1; i <= m ; i++) {
int x,y;
fin >> x >> y;
adj[x].push_back(y);
d[y] ++;
}
queue<int> q;
for (int i = 1 ; i <= n ; i ++) {
if (!d[i]) {
q.push(i);
}
}
int maxi = 0;
int imax = 0;
while (!q.empty()) {
int node = q.front();
q.pop();
ans[node].second = ans[node].first + t[node];
timp = max(timp, ans[node].second);
if (ans[node].second > maxi) {
maxi = ans[node].second;
imax = node;
}
for (auto it : adj[node]) {
if (ans[node].second > ans[it].first) {
ans[it].first = ans[node].second;
p[it] = node;
}
d[it] --;
if (d[it] == 0) {
q.push(it);
}
}
}
fout << "Timp minim " << timp << '\n';
stack<int> s;
while (imax != -1 ) {
s.push(imax);
imax = p[imax];
}
fout << "Activitati critice: ";
while (!s.empty()) {
fout << s.top() << " ";
s.pop();
}
fout << '\n';
for (int i = 1; i <= n ; i++) {
fout << i << ": " << ans[i].first << " " << ans[i].second << '\n';
}
}
void diskstra1() {
int n, m;
vector<vector<pair<int,int>>> adj(250005);
vector<int> vis(50005,0);
vector<int> p(50005,-1);
vector<int> d(50005,INT_MAX);
priority_queue<tuple<int,int,int>, vector<tuple<int,int,int>>, greater<tuple<int,int,int>>> pq;
fin >> n >> m;
for (int i = 1 ; i <= m ; i ++) {
int x,y,c;
fin >> x >> y >> c;
adj[x].push_back({y,c});
adj[y].push_back({x,c});
}
// vector<int> pct_c;
// int pct_n;
// fin >> pct_n;
// for (int i = 0 ; i < pct_n ; i ++) {
// int x;
// fin >> x;
// pct_c.push_back(x);
// }
int start = 1;
//fin >> start;
for (auto it : adj[start]) {
int cost = it.second;
int to = it.first;
pq.push({cost, to, start});
}
vis[start] = 1;
d[start] = 0;
while (!pq.empty()) {
auto [cost,to,from] = pq.top();
pq.pop();
if (!vis[to]) {
d[to] = cost;
vis[to] = 1;
for (auto it : adj[to]) {
if (it.second + cost < d[it.first]) {
pq.push({it.second + cost, it.first, to});
}
}
p[to] = from;
}
}
for (int i = 2 ; i <= n; i++) {
if (d[i] == INT_MAX ) {
fout << 0 << " ";
}else {
fout << d[i] << " ";
}
}
}
int main() {
diskstra1();
}