# Light OJ 1108 – Instant View of Big Bang

0

Algorithm:

1. Make a graph by reverse the edge direction.
2. Run Bellman Ford in reverse Graph and check if there exist a negative weight cycle. Reversing edge doesn’t affect in cycle because if we reverse the edge direction then cycle remain unchanged.
3. If exist a negative cycle then run a DFS on the reverse graph for finding the nodes which are reachable from negative cycle and store them in a vector and after sorting the vector print them.
4. If there is no negative cycle then print impossible.

```
/*
+-+ +-+ +-+
|R| |.| |S|
+-+ +-+ +-+
*/

#include &lt;bits/stdc++.h&gt;

#define pii             pair &lt;int,int&gt;
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout&lt;&lt;#x &quot; = &quot;&lt;&lt;(x)&lt;&lt;endl
#define VI              vector &lt;int&gt;
#define DBG             pf(&quot;Hi\n&quot;)
#define MOD             100007
#define MAX             1005
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf(&quot;%d&quot;,&amp;a)
#define sff(a,b)        scanf(&quot;%d%d&quot;,&amp;a,&amp;b)
#define sfff(a,b,c)     scanf(&quot;%d%d%d&quot;,&amp;a,&amp;b,&amp;c)
#define loop(i,n)       for(int i=0;i&lt;n;i++)
#define REP(i,a,b)      for(int i=a;i&lt;b;i++)
#define TEST_CASE(t)    for(int z=1;z&lt;=t;z++)
#define PRINT_CASE      printf(&quot;Case %d: &quot;,z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;

struct data
{
int u,v,w;
data(int x, int y, int z)
{
u=x,v=y,w=z;
}
};

int n,m,total_edge;
vector&lt;data&gt; graph;
vector&lt;int&gt;ans,reverse_graph[MAX];
map&lt;int,bool&gt;mp;
int d[MAX];
bool visited[MAX];

void dfs(int u)
{
mp[u]=1;
ans.pb(u);
loop(i,SZ(reverse_graph[u]))
{
int v=reverse_graph[u][i];
if(!mp[v])
dfs(v);
}
}

bool bellmanford()
{
for(int i=1;i&lt;n;i++)
{
loop(i,m)
{
int u=graph[i].u;
int v=graph[i].v;
if(d[u]+graph[i].w&lt;d[v])
{
d[v]=d[u]+graph[i].w;
}
}
}

bool negative_cycle=0;

loop(i,m)
{
int u=graph[i].u;
int v=graph[i].v;
if(d[u]+graph[i].w&lt;d[v])
{
negative_cycle=1;
d[v]=d[u]+graph[i].w;
if(!mp[u])
dfs(u);
}
}
return negative_cycle;
}

void allclear()
{
graph.clear();
ans.clear();
mp.clear();

loop(i,n+2)
{d[i]=inf;reverse_graph[i].clear();}
}

int main()
{
///freopen(&quot;in.txt&quot;,&quot;r&quot;,stdin);
///freopen(&quot;out.txt&quot;,&quot;w&quot;,stdout);
int t;
sf(t);
TEST_CASE(t)
{
int u,v,w;
sff(n,m);
allclear();
loop(i,m)
{
sfff(u,v,w);
graph.pb(data(v,u,w));//reversing graph
reverse_graph[v].pb(u);
}

PRINT_CASE;
if(bellmanford())
{
sort(all(ans));
pf(&quot;%d&quot;,ans[0]);
REP(i,1,SZ(ans))
pf(&quot; %d&quot;,ans[i]);
pf(&quot;\n&quot;);
}
else
pf(&quot;impossible\n&quot;);
}
return 0;
}

```

# Light OJ 1221 – Travel Company (Bellman Ford)

1

```
/*
+-+ +-+ +-+
|R| |.| |S|
+-+ +-+ +-+
*/

#include &lt;bits/stdc++.h&gt;

#define pii             pair &lt;int,int&gt;
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout&lt;&lt;#x &quot; = &quot;&lt;&lt;(x)&lt;&lt;endl
#define VI              vector &lt;int&gt;
#define DBG             pf(&quot;Hi\n&quot;)
#define MOD             100007
#define MAX             105
#define CIN             ios_base::sync_with_stdio(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf(&quot;%d&quot;,&amp;a)
#define sff(a,b)        scanf(&quot;%d%d&quot;,&amp;a,&amp;b)
#define sfff(a,b,c)     scanf(&quot;%d%d%d&quot;,&amp;a,&amp;b,&amp;c)
#define loop(i,n)       for(int i=0;i&lt;n;i++)
#define REP(i,a,b)      for(int i=a;i&lt;b;i++)
#define TEST_CASE(t)    for(int z=1;z&lt;=t;z++)
#define PRINT_CASE      printf(&quot;Case %d: &quot;,z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;

vector&lt;int&gt;from,to,cost;
int d[MAX];

int main()
{
///freopen(&quot;in.txt&quot;,&quot;r&quot;,stdin);
///freopen(&quot;out.txt&quot;,&quot;w&quot;,stdout);
int t;
sf(t);
TEST_CASE(t)
{
int n,r,p;
sfff(n,r,p);
int a,b,income,expence;
loop(i,r)
{
sc(&quot;%d %d %d %d&quot;,&amp;a,&amp;b,&amp;income,&amp;expence);
from.pb(a);
to.pb(b);
cost.pb(p*expence-income);
}

bool test=0;
for(int j=1;j&lt;=n;j++)
{
for(int i=0;i&lt;r;i++)
{
int temp=d[from[i]]+cost[i];
if(temp&lt;d[to[i]])
{
if(j==n) test=1;
d[to[i]]=temp;
}
}
}

PRINT_CASE;
if(test)
pf(&quot;YES\n&quot;);
else
pf(&quot;NO\n&quot;);

ms(d,0);
from.clear();
to.clear();
cost.clear();
}
return 0;
}

```

This problem can be solved using Floyed Warshal algorithm. Another solution is Here Light OJ 1221 – Travel Company (Floyed Warshal)

# Light OJ 1074 – Extended Traffic

0

```
/*
+-+ +-+ +-+
|R| |.| |S|
+-+ +-+ +-+
*/

#include &lt;bits/stdc++.h&gt;

#define pii             pair &lt;int,int&gt;
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout&lt;&lt;#x &quot; = &quot;&lt;&lt;(x)&lt;&lt;endl
#define VI              vector &lt;int&gt;
#define DBG             pf(&quot;Hi\n&quot;)
#define MOD             100007
#define MAX             202
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf(&quot;%d&quot;,&amp;a)
#define sff(a,b)        scanf(&quot;%d%d&quot;,&amp;a,&amp;b)
#define sfff(a,b,c)     scanf(&quot;%d%d%d&quot;,&amp;a,&amp;b,&amp;c)
#define loop(i,n)       for(int i=0;i&lt;n;i++)
#define REP(i,a,b)      for(int i=a;i&lt;b;i++)
#define TEST_CASE(t)    for(int z=1;z&lt;=t;z++)
#define PRINT_CASE      printf(&quot;Case %d:\n&quot;,z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;

int n,m;
int weight[MAX], d[MAX];
vector&lt;int&gt;u,v,cost,graph[MAX];
void allclear()
{
loop(i,n+1)
{
weight[i]=0;
d[i]=inf;
}
d[1]=0;
u.clear();
v.clear();
cost.clear();
}

int main()
{
///freopen(&quot;in.txt&quot;,&quot;r&quot;,stdin);
///freopen(&quot;out.txt&quot;,&quot;w&quot;,stdout);
int t;
sf(t);
TEST_CASE(t)
{
sf(n);
allclear();
for(int i=1;i&lt;=n;i++) sf(weight[i]);
sf(m);
int x,y;
loop(i,m)
{
sff(x,y);
u.pb(x);
v.pb(y);
int xx=weight[y]-weight[x];
cost.pb(xx*sqr(xx));
}

for(int i=1;i&lt;=n;i++)
{
bool update=0;
loop(i,m)
{
int x=u[i];
int y=v[i];
if(d[x]+cost[i]&lt;d[y])
{
update=1;
d[y]=d[x]+cost[i];
}
}
if(update==0)
break;
}
PRINT_CASE;
int p,q;
sf(q);
loop(i,q)
{
sf(p);
if(d[p]&lt;3 ||d[p]&gt;100000)
pf(&quot;?\n&quot;);
else
pf(&quot;%d\n&quot;,d[p]);
}
}
return 0;
}

```