Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define MAX 100007
- using namespace std;
- typedef pair<int, int> pii;
- int n, m;
- int dist[MAX];
- vector<pii> graph[MAX];
- void dijkstra(int no){
- priority_queue<pii, vector<pii> > q;
- dist[no] = 0;
- q.push(make_pair(0, no));
- while(!q.empty()){
- pii top = q.top();
- q.pop();
- int val = top.first;
- int v = top.second;
- //cout << val << " " << v << endl;
- for(int i = 0; i < graph[v].size(); i++) {
- int filho = graph[v][i].second;
- int valor = graph[v][i].first;
- //cout << " " << valor << " " << filho << endl;
- if(dist[filho] > (dist[v] + valor)){
- dist[filho] = dist[v] + valor;
- q.push(make_pair(dist[filho], filho));
- }
- }
- }
- }
- int main(){
- scanf("%d %d", &n, &m);
- for(int i = 0; i < m; i++){
- int a, b, val;scanf("%d %d %d", &a, &b, &val);
- graph[a].push_back(make_pair(val, b));
- graph[b].push_back(make_pair(val, a));
- }
- for(int i = 0; i <= n; i++) dist[i] = 100000007;
- int val; scanf("%d", &val);
- dijkstra(val);
- //for(int i = 1; i <= n; i++) cout << dist[i] << " ";
- //cout << endl;
- int menor = 100000007, maior = 0;
- for(int i = 1; i <= n; i++){
- if(i != val){
- menor = min(menor, dist[i]);
- maior = max(maior, dist[i]);
- }
- }
- printf("%d\n", maior-menor);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment