রবিবার, ২৮ মে, ২০১৭

Maxmum path by using dijkstra ( node 0 to node n-1 )

//input
/*4
0 1 4
0 2 2
2 3 3
output=5=(2+3)*/

#include <bits/stdc++.h>
using namespace std;

//#define maximum 10000
//#define INF 1000000
vector <int> edges[1000];
vector <int> cost[1000];
int N, E;

struct data {
    int city, dist;
    bool operator < ( const data& p) const {
        return dist < p.dist;
    }
};

int dijsktra(int source, int destination)
{
    priority_queue <data> q;
    vector <int> distance;

    for(int i = 0; i <= N; i++)
        distance.push_back(-1);

    int maxi = -1;

    data u, v;
    u.city = source, u.dist = 0;
    q.push(u);
    distance[source] = 0;
    while(!q.empty()) {
        u = q.top();
        int ucost = distance[u.city];
        q.pop();
        //if(u.city == destination)
           // return distance[u.city];
        int sz = edges[u.city].size();
        for(int i = 0; i < sz; i++) {
            v.city = edges[u.city][i], v.dist = cost[u.city][i] + ucost;
            if(distance[v.city] == -1) {
                distance[v.city] = v.dist;
                q.push(v);
                maxi = max(maxi, v.dist);
            }
        }
    }
    return maxi;
}

int main()
{
    scanf("%d", &N);

    for(int i = 1; i < N; i++) {
        int x, y, c;
        scanf("%d %d %d", &x, &y, &c);
        edges[x].push_back(y);
        edges[y].push_back(x);
        cost[x].push_back(c);
        cost[y].push_back(c);
    }

    //int source, destination;
    //scanf("%d %d", &source, &destination);
    int maximum_cost = dijsktra(0, N-1);

    cout << maximum_cost << endl;
    return 0;
}

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন

Factory Pattern

Factory Method  is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alte...