Page 1 of 2

### 11857 - Driving Range

Posted: Mon Oct 04, 2010 7:42 am
I think this is simple DFS problem.
I called a dfs then check which road length is maximum
Is my approach is ok?

PLZ give me some critical input output....

### Re: 11857 - Driving Range

Posted: Mon Oct 04, 2010 8:33 am

### Re: 11857 - Driving Range

Posted: Mon Oct 04, 2010 1:52 pm
I don't know why but I'm not able to understand those graph problems. Do you think i am the problem or problem description is not so good ? And can you give me some guide to improve the ability of understanding the problem.

### Re: 11857 - Driving Range

Posted: Mon Oct 04, 2010 2:39 pm
Solve,solve and solve......

### Re: 11857 - Driving Range

Posted: Mon Oct 04, 2010 5:13 pm
In my code I have used prims algorithm to find MST. and the edge with maximum length is the output. I am getting TLE..
I submitted my solution without calling the function to find MST (func name is dfs) still i got TLE.. so i think the problem is in variable declaration (for adjacency matrix) very unlikely though.... which i have implemented using array of vectors to make a 2D array..
the input range was 1 million. are there any other alternative of making an adjacency matrix.

Code: Select all

``````
using namespace std;
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <fstream>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
//BEGIN_TEMPLATE_BY_PRATYUSH_VERMA

//CONSTANT
#define INF (1<<31)-1
#define PI 3.1428571428571428571428571428571

//FUNC
#define MAX(i,j) (i)>(j)?(i):(j)
#define MIN(i,j) (i)<(j)?(i):(j)
#define REP(i,a) for((i)=0;(i)<(a);(i)++)
#define REP_(i,a) for((i)=0;(i)<=(a);(i)++)
#define FOR(i,a,b) for((i)=(a);(i)<(b);(i)++)
#define FOR_(i,a,b) for((i)=(a);(i)<=(b);(i)++)
#define VAR(V,init) __typeof(init) V=(init)
#define FOREACH(I,C) for(VAR(I,(C).begin());I!=(C).end();I++)
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((int)(x.size()))
#define LENGTH(x) ((int)(x.length()))
#define SZ(x) sizeof(x)
#define MEM(m,i) memset((m),(i),SZ(m))
#define PB(x,y) (x).push_back(y)
#define MP(x,y) make_pair(x,y)
#define V(x) vector < x >

typedef pair<int,int>   PII;
typedef pair<char,int>  PCI;
typedef pair<int,PII>   TRI;
typedef V( int )        VI;
typedef V( PII )        VII;
typedef V( TRI )        VTRI;
typedef V( string )     VS;
typedef long long       LL;
typedef long double     LD;

inline string i2s(int number) { stringstream ss; ss << number; return ss.str(); }
inline void input( int &n ) { n=0; int ch=getchar(); while( ch < '0' || ch > '9' ) ch=getchar(); while( ch >= '0' && ch <= '9' ) n = (n<<3)+(n<<1) + ch-'0', ch=getchar(); }
bool vis[1000000];
int m,n;
//END_TEMPLATE_BY_PRATYUSH_VERMA

struct node {
int i;
node (int a) {i=a;}
friend bool operator< (const node &a, const node &b)
{
return (a.i>b.i);
}

};

int dfs()
{
int min=0;
priority_queue<node> s;
s.push(node (0));
int i;
while(!s.empty())
{
node t=s.top();
s.pop();
vis[t.i]=true;
REP(i,n)
{
if(vis[i]==true || t.i==i) continue;
s.push(node (i));
}
}
REP(i,n)
if(!vis[i]) return -1;
return min;
}
int main()
{
int i,j,k,p,q;
while(1)
{
input(n);
input(m);
if(m==0 && n==0) return 0;
if(m<n-1)
{
printf("IMPOSSIBLE\n");
continue;
}
MEM(vis,0);
REP(i,n)
REP(i,m)
{
input(p);
input(q);
input(k);
//            cin>>p>>q>>k;
}
int flag=dfs();
if(flag==-1) printf("IMPOSSIBLE\n");
else
{
printf("%d\n",flag);
}
}
return 0;
}
``````

### Re: 11857 - Driving Range

Posted: Mon Oct 04, 2010 8:13 pm
Here you find a data that needed!

### Re: 11857 - Driving Range

Posted: Mon Oct 04, 2010 10:09 pm
ooh.. got it thanks..

### Re: 11857 - Driving Range

Posted: Tue Oct 05, 2010 8:31 pm
Now i got accepted

but my runtime is 2.904
I use Kruskal + dfs.
can anyone give me any hints to decrease my runtime....

### Re: 11857 - Driving Range

Posted: Wed Oct 06, 2010 1:14 am
durjay wrote:Now i got accepted

but my runtime is 2.904
I use Kruskal + dfs.
can anyone give me any hints to decrease my runtime....
I used Disjoint Sets to solve the problem. My running time is 0.684 sec.

### Re: 11857 - Driving Range

Posted: Thu Nov 04, 2010 4:39 am
getting wa pls help

Code: Select all

``````got ac code removed :)

``````

### Re: 11857 - Driving Range

Posted: Thu Nov 04, 2010 6:01 am

### Re: 11857 - Driving Range

Posted: Thu Nov 04, 2010 6:07 am
Hafiz in 67th line you used
if(u != v)
but it will be
if(x!=y)

### Re: 11857 - Driving Range

Posted: Thu Nov 04, 2010 7:58 am
thanks

### Re: 11857 - Driving Range

Posted: Tue Jan 04, 2011 6:51 pm
Sorry for Silly Post..I designed a way to solve this problem using DFS.But I dont know which data structure is to use for representing
the Graph.Because size is too much(1 million).I code in c/c++ but array,structure of c/c++ cant support this huge memory.So how to represent graph?

### Re: 11857 - Driving Range

Posted: Thu Jan 06, 2011 3:24 pm
You can use adjacent list or structure ot STL container like as vector