Pagini recente » Cod sursa (job #1652236) | Cod sursa (job #744582) | Cod sursa (job #2884471) | Cod sursa (job #422598) | Cod sursa (job #631431)
Cod sursa(job #631431)
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 "dijkstra.in"
#define 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 CompareHeap {
private:
vector<ll> pc;
public:
CompareHeap(const vector<ll>& pc) {
this->pc = pc;
}
bool operator()(const int x, const int y) const
{
return pc[x] < pc[y] ;
}
};
class Dijkstra {
private:
int source;
vector<ll> pathCost;
vector<ll> nodeParent;
vector<pi> graph[N_MAX];
int nNode;
void addRec(int dest,VI& path) {
if(dest==-1)
return;
addRec(nodeParent[dest],path);
path.pb(dest);
}
public:
Dijkstra(int nNode) {
this->nNode = nNode;
pathCost = vector<ll>(nNode);
nodeParent = vector<ll>(nNode);
FORit(it, pathCost)
(*it) = oo;
FORit(it, nodeParent)
(*it) = -1;
}
void addEdge(int src,int dest,int cost) {
graph[src].pb(mp(dest,cost));
}
void addSource(int node) {
this->source = node;
}
ll getMinCost(int dest) {
return pathCost[dest];
}
VI getPathFromSource(int dest) {
VI rec;
addRec(dest,rec);
return rec;
}
II void run() {
vector<int> heap;
vector<bool> inheap = vector<bool>(nNode);
pathCost[source] = 0;
heap.pb(source);
inheap[source]=true;
int heapSize = 1;
CompareHeap heapCompare(pathCost);
while(heapSize) {
int node = heap.front();
pop_heap(heap.begin(),heap.begin()+heapSize,heapCompare);
heapSize--;
inheap[node] = false;
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]) {
if(heapSize<Size(heap))
(*(heap.begin()+heapSize))=(*it).f;
else
heap.pb((*it).f);
heapSize++;
push_heap(heap.begin(),heap.begin()+heapSize,heapCompare);
}
else {
make_heap(heap.begin(),heap.begin()+heapSize,heapCompare);
}
}
}
}
}
~Dijkstra() {
}
};
void scan() {
int N,M;
freopen(IN,"r",stdin);
freopen(OUT,"w",stdout);
scanf("%d%d",&N,&M);
Dijkstra dij(N+1);
for(int i=0;i<M;i++) {
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
dij.addEdge(x,y,c);
}
dij.addSource(1);
dij.run();
FOR(i,2,N)
if(dij.getMinCost(i)==oo)
printf("0 ");
else
printf("%lld ",dij.getMinCost(i));
}
int main()
{
scan();
return 0;
}