Light OJ 1108 – Instant View of Big Bang

0

Problem Link : http://www.lightoj.com/volume_showproblem.php?problem=1108

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 <bits/stdc++.h>

#define pii             pair <int,int>
#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<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#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("%d",&a)
#define sff(a,b)        scanf("%d%d",&a,&b)
#define sfff(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",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<data> graph;
vector<int>ans,reverse_graph[MAX];
map<int,bool>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<n;i++)
    {
        loop(i,m)
        {
            int u=graph[i].u;
            int v=graph[i].v;
            if(d[u]+graph[i].w<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<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("in.txt","r",stdin);
    ///freopen("out.txt","w",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("%d",ans[0]);
            REP(i,1,SZ(ans))
            pf(" %d",ans[i]);
            pf("\n");
        }
        else
            pf("impossible\n");
    }
    return 0;
}


Advertisements

Light OJ 1221 – Travel Company (Bellman Ford)

1

Problem Link : http://www.lightoj.com/volume_showproblem.php?problem=1221


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

#include <bits/stdc++.h>

#define pii             pair <int,int>
#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<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#define MOD             100007
#define MAX             105
#define CIN             ios_base::sync_with_stdio(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf("%d",&a)
#define sff(a,b)        scanf("%d%d",&a,&b)
#define sfff(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d: ",z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;

vector<int>from,to,cost;
int d[MAX];

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",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("%d %d %d %d",&a,&b,&income,&expence);
            from.pb(a);
            to.pb(b);
            cost.pb(p*expence-income);
        }

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

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

        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

Problem Link : http://www.lightoj.com/volume_showproblem.php?problem=1074


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

#include <bits/stdc++.h>

#define pii             pair <int,int>
#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<<#x " = "<<(x)<<endl
#define VI              vector <int>
#define DBG             pf("Hi\n")
#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("%d",&a)
#define sff(a,b)        scanf("%d%d",&a,&b)
#define sfff(a,b,c)     scanf("%d%d%d",&a,&b,&c)
#define loop(i,n)       for(int i=0;i<n;i++)
#define REP(i,a,b)      for(int i=a;i<b;i++)
#define TEST_CASE(t)    for(int z=1;z<=t;z++)
#define PRINT_CASE      printf("Case %d:\n",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<int>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("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t;
    sf(t);
    TEST_CASE(t)
    {
        sf(n);
        allclear();
        for(int i=1;i<=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<=n;i++)
        {
            bool update=0;
            loop(i,m)
            {
                int x=u[i];
                int y=v[i];
                if(d[x]+cost[i]<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]<3 ||d[p]>100000)
                pf("?\n");
            else
                pf("%d\n",d[p]);
        }
    }
    return 0;
}