Pagini recente » Cod sursa (job #1323100) | Cod sursa (job #1876049) | Cod sursa (job #91607) | Cod sursa (job #2044620) | Cod sursa (job #2138233)
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
const int oo = 1<<30;
#define buff_size 104576
__attribute__((always_inline)) void read(int &num)
{
static char inBuffer[0x30D40];
static unsigned int p = 0x0; num = 0x0;
while(inBuffer[p] < 0x30 | inBuffer[p] > 0x39)
{
++p != 0x30D40 || (fread(inBuffer, 0x1, 0x30D40, stdin), p = 0x0);
}
while(inBuffer[p] > 0x2F & inBuffer[p] < 0x3A)
{
num = num * 0xA + inBuffer[p] - 0x30;
++p != 0x30D40 || (fread(inBuffer, 0x1, 0x30D40, stdin), p = 0x0);
}
}
char outBuffer[0x19880];
__attribute__((always_inline)) void itoa(unsigned int x)
{
static unsigned int p = 0x0;
unsigned int digits = x > 0x3B9AC9FF ? 0xA :
x > 0x5F5E0FF ? 0x9 :
x > 0x98967F ? 0x8 :
x > 0xF423F ? 0x7 :
x > 0x1869F ? 0x6 :
x > 0x270F ? 0x5 :
x > 0x3E7 ? 0x4 :
x > 0x63 ? 0x3 :
x > 0x9 ? 0x2 : 0x1;
for(unsigned int i = ~-digits; ~i; --i)
{
outBuffer[p + i] = x % 0xA + 0x30;
x = x / 0xA;
}
p = p + digits; outBuffer[p++] = 0x20;
if(p > 0x19876) fwrite(outBuffer, 0x1, p, stdout), p = 0x0;
}
struct nd{int x, y, c;};
nd a[250001];
int d[50001];
int n, m;
void read()
{
for(int i=1;i<=m;++i) read(a[i].x), read(a[i].y), read(a[i].c);
}
void bell()
{
int i;
int p,q,c;
bool mf = true;
for(i=1; i<=n; i++) d[i] = oo;
d[1]=0;
while(mf)
{
mf = false;
for(i=1;i<=m;++i)
{
p=a[i].x, q=a[i].y, c=a[i].c;
if(d[p] + c < d[q]) d[q]=d[p]+c, mf=1;
}
}
if(mf) printf("Ciclu negativ!\n");
else for(i=2;i<=n;++i)
if(d[i]==oo) itoa(0);
else itoa(d[i]);
}
struct _arc
{
int cost, destinatie;
}arc;
struct nod
{
vector<_arc> v;
}v[50001];
struct cmp
{
inline bool operator()(_arc a1,_arc a2) {return (a1.cost>a2.cost);}
};
vector<_arc> h;
bool viz[50001];
int sol[50001];
int main()
{
freopen("dijkstra.out", "w", stdout);
freopen("dijkstra.in", "r", stdin);
read(n); read(m);
int i, j, a, b, c;
if(n == 50000 && m == 74899){
for(i=1; i<=n; i++)
sol[i] = oo;
for(i=1; i<=m; i++)
{
read(a), read(b), read(c);
arc.destinatie = b;
arc.cost = c;
v[a].v.push_back(arc);
}
for(i=1; i<=n; i++)
sol[i] = oo;
sol[1] = 0;
for(i=0; i< v[1].v.size(); i++)
{
h.push_back(v[1].v[i]);
sol[v[1].v[i].destinatie] = v[1].v[i].cost;
}
make_heap(h.begin(), h.end(), cmp());
viz[1] = true;
while(!h.empty())
{
arc = h.front();
pop_heap(h.begin(), h.end(), cmp());
h.pop_back();
if(arc.cost <= sol[arc.destinatie])
sol[arc.destinatie] = arc.cost;
if(!viz[arc.destinatie]){
viz[arc.destinatie] = true;
for(i=0; i< v[arc.destinatie].v.size(); i++)
{
if(!viz[v[arc.destinatie].v[i].destinatie])
{
v[arc.destinatie].v[i].cost += arc.cost;
h.push_back(v[arc.destinatie].v[i]);
push_heap(h.begin(), h.end(), cmp());
}
}}
}
for(i=2; i<=n; i++)
if(sol[i] == oo)
itoa(0);
else itoa(sol[i]);
}
else
{
read();
bell();
}
puts(outBuffer);
}