Commit 63b3a0ff authored by userresu's avatar userresu

changed critical problem code

parent 7e269444
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define NILL (-1)
#define INF ((unsigned int) 0x7FFFFFFF)
#define MAXN 50000
#define MAXM 400000
/* Data type definitions for graph representation */
typedef struct _edge {
int u, v, len;
} Edge;
Edge E[MAXM];
typedef struct _node {
int id, len;
struct _node *next;
} Node;
Node *G[MAXN];
typedef struct _nodeinfo {
int id, dist;
} NInfo;
NInfo V[MAXN];
int Loc[MAXN], D[MAXN], Cin[MAXN];
/* ------------------ Fast Read ------------------------ */
#define BSIZE 1<<15
char buffer[BSIZE];
int bpos = 0L, bsize = 0L;
int readInt()
{
int d = 0L, x = 0L;
char c;
while (1) {
if (bpos >= bsize) {
bpos = 0;
if (feof(stdin)) return x;
bsize = fread(buffer, 1, BSIZE, stdin);
}
c = buffer[bpos++];
if (c >= '0' && c <= '9') { x = x*10 + (c-'0'); d = 1; }
else if (d == 1) return x;
}
return -1;
}
/* ------------------ Fast Read ------------------------ */
Node *initNode(int id, int len)
{
Node *v;
if ((v =(Node *)malloc(sizeof(Node))) == NULL) {
fprintf(stderr, "Out of memory!\n");
exit(1);
}
v->id = id; v->len = len; return(v);
}
void readGraph(int N, int M)
{
Node *new;
int i, x, y, len; //, best=0;
for (i = 0; i < N; i++) G[i] = NULL;
for (i = 0; i < M; i++) {
// scanf("%d %d %d\n", &x, &y, &len);
x = readInt(); y = readInt(); len = readInt();
// if (best < len) best=len;
E[i].u = --x; E[i].v = --y; E[i].len = len;
new = initNode(y, len); new->next = G[x]; G[x] = new;
}
// fprintf(stderr, "(max len = %d)\n", best);
}
/* ------------------------------------------------- */
/* Priority queue implementation using a binary heap */
int heapsize;
void swapNodes(NInfo A[], int Loc[], int i, int j)
{
NInfo tmp = A[i]; A[i] = A[j]; A[j] = tmp;
Loc[A[i].id] = i; Loc[A[j].id] = j;
}
void createHeap(NInfo A[], int Loc[], int N)
{
int i; heapsize = N-1;
for (i = (N-1)/2; i >= 0; i--)
fixHeap(A, Loc, i);
}
int fixHeap(NInfo A[], int Loc[], int i)
{
int left = 2*i+1, right = left+1, smallest = i;
if ((left <= heapsize) && (A[left].dist < A[i].dist)) smallest = left;
if ((right <= heapsize) && (A[right].dist < A[smallest].dist)) smallest = right;
if (smallest != i) {
swapNodes(A, Loc, i, smallest);
fixHeap(A, Loc, smallest);
}
}
int extractMin(NInfo A[], int Loc[])
{
int res = A[0].id;
swapNodes(A, Loc, 0, heapsize);
heapsize--; fixHeap(A, Loc, 0);
return(res);
}
void decreasePriority(NInfo A[], int Loc[], int i)
{
int parent = (i-1)/2;
while ((i > 0) && (A[parent].dist > A[i].dist)) {
swapNodes(A, Loc, i, parent);
i = parent; parent = (i-1)/2;
}
}
int emptyHeap()
{
if (heapsize >=0) return(0);
else return(1);
}
/* End of priority queue implementation */
/* ------------------------------------------------- */
void Dijkstra(int N, int M, int *wcrit, int *crit)
{
Node *v;
int i, u, locu;
int tmp;
for (i = 0; i < N; i++) {
V[i].dist = INF; V[i].id = Loc[i] = i; Cin[i] = 0;
}
V[0].dist = 0;
createHeap(V, Loc, N);
while (!emptyHeap()) {
u = extractMin(V, Loc); locu = Loc[u]; D[u] = V[locu].dist; v = G[u];
// printf("Extracting %3d: (L[%3d] = %d).\n", u, u, V[locu].dist);
while (v != NULL) {
tmp = V[locu].dist + v->len;
if (V[Loc[v->id]].dist > tmp) {
V[Loc[v->id]].dist = tmp;
decreasePriority(V, Loc, Loc[v->id]);
}
v = v->next;
}
}
for (i = 0; i < M; i++) {
if (D[E[i].u] + E[i].len == D[E[i].v]) {
Cin[E[i].v]++; (*wcrit)++;
}
}
for (i = 1; i < N; i++) {
if (Cin[i] == 1) (*crit)++;
if (!Cin[i]) fprintf(stderr, "Node %d unreachable (dist %d)!\n", i, D[i]);
if (D[i] >= INF) fprintf(stderr, "Node %d has infinite (dist %d)!\n", i, D[i]);
}
}
int main()
{
int N, M;
int wc=0, c=0;
#ifdef CONTEST
freopen("critical.in", "rt", stdin);
freopen("critical.out", "wt", stdout);
#endif
scanf("%d %d\n", &N, &M); //fprintf(stderr, "N = %d, M = %d ", N, M);
readGraph(N, M);
Dijkstra(N, M, &wc, &c);
printf("%d %d\n", wc, c);
return 0;
}
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
#define MAXN 100001
int N, M;
vector<vector<int> > con(MAXN), cost(MAXN);
int dist[MAXN], INF = 2000000000, cnt[MAXN];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > Q;
int main () {
#ifdef CONTEST
freopen("critical.in", "rt", stdin);
freopen("critical.out", "wt", stdout);
#endif
scanf("%d %d",&N,&M);
while (M--) {
int x,y,z;
scanf("%d %d %d",&x,&y,&z);
con[x].push_back(y);
cost[x].push_back(z);
}
for (int i=2;i<=N;++i) dist[i] = INF;
Q.push(make_pair(0,1));
while (!Q.empty()) {
pair<int,int> t = Q.top(); Q.pop();
if (t.first > dist[t.second]) continue;
for (int j=0;j<con[t.second].size();++j) {
int x = con[t.second][j];
if (dist[x] > t.first + cost[t.second][j]) {
cnt[x] = 0;
dist[x] = t.first + cost[t.second][j];
Q.push(make_pair(dist[x], x));
}
else if (dist[x] == t.first + cost[t.second][j]) ++cnt[x];
}
}
int a = 0, b = 0;
for (int i=1;i<=N;++i) for (int j=0;j<con[i].size();++j) {
int x = con[i][j];
if (dist[x] == dist[i] + cost[i][j]) {
++a;
if (!cnt[x]) ++b;
}
}
printf("%d %d\n",a,b);
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment