Cod sursa(job #631680)

Utilizator andrei-alphaAndrei-Bogdan Antonescu andrei-alpha Data 9 noiembrie 2011 15:53:31
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.19 kb
using namespace std;

#include <set>
#include <map>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cctype>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <utility>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define oo (1<<30)
#define f first
#define s second
#define II inline
#define db double
#define ll long long
#define pb push_back
#define mp make_pair
#define Size(V) ((int)(V.size()))
#define all(V) (V).begin() , (V).end()
#define CC(V) memset((V),0,sizeof((V)))
#define CP(A,B) memcpy((A),(B),sizeof((B)))
#define FOR(i,a,b) for(int (i)=(a);(i)<=(b);++(i))
#define REP(i, N) for (int (i)=0;(i)<(int)(N);++(i))
#define FORit(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); ++it)

#define IN "code.in" //"dijkstra.in"
#define OUT "code.out" //"dijkstra.out"
#define N_MAX (1<<17)

typedef vector<int> VI;
typedef pair<int,int> pi;
typedef vector<string> VS;
template<class T> string toString(T n) {ostringstream ost;ost<<n;ost.flush();return ost.str();}

//class Dijkstra {
//	public:
		int source;
		int pathCost[N_MAX],nodeParent[N_MAX];
		vector<pi> graph[N_MAX];
        bool inheap[N_MAX];    		
        int nNode;
		
		void addRec(int dest,VI& path) {
			if(dest==-1)
				return;
			addRec(nodeParent[dest],path);
			path.pb(dest);
		}
//	public:
		void init(int pNode) { //Dijkstra(int nNode) {
			nNode = pNode; //this->nNode = nNode;
            CC(inheap);			
            memset(pathCost,100,sizeof(pathCost));
            memset(nodeParent,-1,sizeof(nodeParent));
		}
		void addEdge(int src,int dest,int cost) {
			graph[src].pb( mp(dest,cost) );
		}
		void addSource(int node) {
			source = node;
		}
		int getMinCost(int dest) {
			return pathCost[dest];
		}
		VI getPathFromSource(int dest) {
			VI rec;
			addRec(dest,rec);
			return rec;
		}
        
        struct comp{ bool operator() (int i,int j) { return pathCost[i] > pathCost[j]; } };

        II void run()
        {
                priority_queue<int,VI,comp> Que;

		        pathCost[source] = 0;
                Que.push(source);
				inheap[source]=true;
				
				for(int node;!Que.empty();)
                {
					node = Que.top();
                    inheap[node] = false;
                    Que.pop();
					
					
                    FORit(it,graph[node])
					    if(pathCost[it->f] > pathCost[node] + (*it).s)
                        {
							pathCost[it->f] = pathCost[node] + (*it).s;
							nodeParent[it->f] = node;
							
                            if(!inheap[it->f]) 
							{		
								Que.push(it->f);
                                inheap[it->f] = true;
							}
						}
				}
		}
//		~Dijkstra() {
//		}
//};

void scan() {
	int N,M;
	freopen(IN,"r",stdin);
	freopen(OUT,"w",stdout);
	scanf("%d%d",&N,&M);
	
    init(N + 1); //Dijkstra dij(N+1);
	for(int i=0;i<M;i++) {
		int x,y,c;
		scanf("%d%d%d",&x,&y,&c);
		addEdge(x,y,c);
	}
	addSource(1);
	run();
	FOR(i,2,N)
		if( getMinCost(i)==oo)
			printf("0 ");
		else
			printf("%d ", getMinCost(i)); 

    fprintf(stderr,"Time %d ms\n",(int)(clock() / 1000));
}
int main()
{
	scan();
	return 0;
}