Click here to view the problem
/***
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
struct data
{
int node, cost ;
void MakePair(int i, int j)
{
node = i ;
cost =j ;
}
};
vector<data> jak[10001] ;
int dis[1100] ;
queue<int> q ;
int level[1100];
int Bellmen_ford(int node,int src)
{
memset(dis,999999,sizeof(dis)) ;
memset(level,0,sizeof(level)) ;
dis[src] =0 ;
level[src] =0 ;
q.push(src) ;
int u,v,cost,i,j ;
while(!q.empty())
{
u = q.front() ;
q.pop() ;
if(level[u]==node-1)
break ;
if(!jak[u].empty())
{
for(i=0 ; i<jak[u].size() ; i++)
{
v = jak[u][i].node ;
cost = jak[u][i].cost ;
if(dis[u]+cost < dis[v])
{
dis[v] = dis[u]+cost ;
level[v] = level[u]+1 ;
q.push(v) ;
}
}
}
}
for(i=0 ; i<node ; i++)
{
u =i ;
if(!jak[i].empty())
{
for(j=0 ; j<jak[i].size(); j++)
{
v = jak[u][j].node ;
cost = jak[u][j].cost ;
if(dis[u]+cost<dis[v])
return 1 ;
}
}
}
return 0 ;
}
int main()
{
int test,n,m,x,y,t,i,j ;
sc("%d",&test) ;
data get ;
while(test--)
{
sc("%d %d",&n,&m) ;
for(i=0 ; i<n ; i++)
jak[i].clear() ;
for(i=1 ; i<=m ; i++)
{
sc("%d %d %d",&x,&y,&t) ;
get.MakePair(y,t) ;
jak[x].pb(get) ;
}
if(Bellmen_ford(n,0))
pf("possible\n") ;
else
pf("not possible\n") ;
}
return 0;
}
/***
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
struct data
{
int node, cost ;
void MakePair(int i, int j)
{
node = i ;
cost =j ;
}
};
vector<data> jak[10001] ;
int dis[1100] ;
queue<int> q ;
int level[1100];
int Bellmen_ford(int node,int src)
{
memset(dis,999999,sizeof(dis)) ;
memset(level,0,sizeof(level)) ;
dis[src] =0 ;
level[src] =0 ;
q.push(src) ;
int u,v,cost,i,j ;
while(!q.empty())
{
u = q.front() ;
q.pop() ;
if(level[u]==node-1)
break ;
if(!jak[u].empty())
{
for(i=0 ; i<jak[u].size() ; i++)
{
v = jak[u][i].node ;
cost = jak[u][i].cost ;
if(dis[u]+cost < dis[v])
{
dis[v] = dis[u]+cost ;
level[v] = level[u]+1 ;
q.push(v) ;
}
}
}
}
for(i=0 ; i<node ; i++)
{
u =i ;
if(!jak[i].empty())
{
for(j=0 ; j<jak[i].size(); j++)
{
v = jak[u][j].node ;
cost = jak[u][j].cost ;
if(dis[u]+cost<dis[v])
return 1 ;
}
}
}
return 0 ;
}
int main()
{
int test,n,m,x,y,t,i,j ;
sc("%d",&test) ;
data get ;
while(test--)
{
sc("%d %d",&n,&m) ;
for(i=0 ; i<n ; i++)
jak[i].clear() ;
for(i=1 ; i<=m ; i++)
{
sc("%d %d %d",&x,&y,&t) ;
get.MakePair(y,t) ;
jak[x].pb(get) ;
}
if(Bellmen_ford(n,0))
pf("possible\n") ;
else
pf("not possible\n") ;
}
return 0;
}
No comments:
Post a Comment