Re: 11733 - Airports
Posted: Sun May 25, 2014 6:32 am
Thanks ..got AC .....kindly could you give some other classes that would help me in I/O routines ?lbv wrote:733 - AIRPORT
Thanks ..got AC .....kindly could you give some other classes that would help me in I/O routines ?lbv wrote:733 - AIRPORT
Here's all I could find from my old code: http://pastebin.com/rp6rzUK9Diaa.Abobaraka wrote:kindly could you give some other classes that would help me in I/O routines ?
Your union-find implementation seems slow, turning your program's complexity to O(NM) per test case, which is very high. Try a different implementation; one with path compression, for example.just_yousef wrote:Why TLE![]()
![]()
Code: Select all
1
8 5 15
3 1 45
8 2 25
5 8 8
3 4 15
6 2 8
Code: Select all
Case #1: 106 6
Thank You, Got AClbv wrote:Your union-find implementation seems slow, turning your program's complexity to O(NM) per test case, which is very high. Try a different implementation; one with path compression, for example.just_yousef wrote:Why TLE![]()
![]()
Code: Select all
#include <stdio.h>
#include <string.h>
int size[10000];
int root[10000];
int find(int a){
if(a==root[a])return a;
a=root[a];
return find(a);
}
struct Edge {
int a;
int b;
int w;
};
int comparator(const void *p, const void *q)
{
int l = ((struct Edge *)p)->w;
int r = ((struct Edge *)q)->w;
return (l - r);
}
int main()
{
int T;
int t;
int N;
int M;
int m;
int A;
int x;
int y;
int z;
int q;
int ans;
int ports;
int a;
int b;
struct Edge PQ[10000];
scanf("%d", &T);
for(t = 1; t <= T; t++){
scanf("%d", &N);
scanf("%d", &M);
scanf("%d", &A);
int i;
for(i = 0; i < N; i++){
size[i] = 1;
root[i] = i;
}
for(m = 0; m < 10000; m++){
PQ[m].a = 11000;
PQ[m].b = 11000;
PQ[m].w = 11000;
}
for(m = 0; m < M; m++){
scanf("%d", &x);
scanf("%d", &y);
scanf("%d", &z);
PQ[m].a = x;
PQ[m].b = y;
PQ[m].w = z;
}
qsort(PQ, 10000, sizeof(struct Edge), comparator);
q = 0;
ans = 0;
for(m = 0; m < M; m++){
a=PQ[m].a;
b=PQ[m].b;
z=PQ[m].w;
a=find(a);
b=find(b);
if(a!=b && z < A){
q++;
ans = ans + PQ[m].w;
if(size[a]>=size[b]){
size[a] = size[a] + size[b];
root[b] = a;
}
else{
size[b]= + size[b]+ size[a];
root[a] = b;
}
if(q == N-1) break;
}
}
ports = 0;
ports = N-q;
ans = ans + ports*A;
printf("Case #%d: %d %d\n", t, ans, ports);
}
return 0;
}