Sunday, January 6, 2019

[UVa] 11090 - Going in Cycle!!

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : 11090 - Going in Cycle!!
Source            : UVa Online Judge
Category          : Graph Theory
Algorithm         : Karp's minimum mean weight cycle algorithm
Verdict           : Accepted
#include "bits/stdc++.h"
 
using namespace std;
 
#define FI              freopen("in.txt", "r", stdin)
#define FO              freopen("out.txt", "w", stdout)
#define FAST            ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
 
#define FOR(i, n)       for (int i = 1; i <= n; i++)
#define For(i, n)       for (int i = 0; i < n; i++)
#define ROF(i, n)       for (int i = n; i >= 1; i--)
#define Rof(i, n)       for (int i = n-1; i >= 0; i--)
#define FORI(i, n)      for (auto i : n)
 
#define ll              long long
#define ull             unsigned long long
#define vi              vector <int>
#define vl              vector <ll>
#define pii             pair <int, int>
#define pll             pair <ll, ll>
#define mk              make_pair
#define ff              first
#define ss              second
#define eb              emplace_back
#define em              emplace
#define pb              push_back
#define ppb             pop_back
#define All(a)          a.begin(), a.end()
#define memo(a, b)      memset(a, b, sizeof a)
#define Sort(a)         sort(All(a))
#define ED(a)           Sort(a), a.erase(unique(All(a)), a.end())
#define rev(a)          reverse(All(a))
#define sz(a)           (int)a.size()
#define max3(a, b, c)   max(a, max(b, c))
#define min3(a, b, c)   min(a, min(b, c))
#define maxAll(a)       *max_element(All(a))
#define minAll(a)       *min_element(All(a))
#define allUpper(a)     transform(All(a), a.begin(), :: toupper)
#define allLower(a)     transform(All(a), a.begin(), :: tolower)
#define endl            '\n'
#define nl              puts("")
#define ub              upper_bound
#define lb              lower_bound
#define Exp             exp(1.0)
#define PIE             2*acos(0.0)
#define Sin(a)          sin(((a)*PIE)/180.0)
#define EPS             1e-9
 
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
using namespace __gnu_pbds;
 
template <typename T> using orderset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
 
#include "ext/rope"
using namespace __gnu_cxx;
 
// rope <int> Rope;
 
// int dr[] = {1, -1, 0, 0}; // 4 Direction
// int dc[] = {0, 0, 1, -1};
// int dr[] = {0, 0, 1, -1, 1, 1, -1, -1}; // 8 Direction
// int dc[] = {1, -1, 0, 0, 1, -1, 1, -1};
// int dr[] = {-1, 1, -2, -2, -1, 1, 2, 2}; // knight Moves
// int dc[] = {-2, -2, -1, 1, 2, 2, 1, -1};
 
#define trace1(x)                           cerr << #x << ": " << x << endl;
#define trace2(x, y)                        cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                     cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)                  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)               cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)            cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
 
inline int setbit(int mask, int pos)        { return mask |= (1 << pos); }
inline int resetbit(int mask, int pos)      { return mask &= ~(1 << pos); }
inline int togglebit(int mask, int pos)     { return mask ^= (1 << pos); }
inline bool checkbit(int mask, int pos)     { return (bool)(mask & (1 << pos)); }
 
#define popcount(mask)                       __builtin_popcount(mask) // count set bit
#define popcountLL(mask)                     __builtin_popcountll(mask) // for long long
 
inline int read()                           { int a; scanf("%d", &a); return a; }
inline ll readLL()                          { ll a; scanf("%lld", &a); return a; }
inline double readDD()                      { double a; scanf("%lf", &a); return a; }
 
template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); }
int toInt(string s)                          { int num; istringstream iss(s); iss >> num; return num;  }
ll toLLong(string s)                         { ll num; istringstream iss(s); iss >> num; return num; }
 
#define inf             123456789
#define mod             1000000007
 
static const int maxn = 100 + 5;
static const int logn = 18;
 
struct node
{
    int v, w;
    node() {}
    node(int v = 0, int w = 0) : v(v), w(w) {}
};
 
struct information
{
    int node, finishTime;
    information(int node = 0, int finishTime = 0) : node(node), finishTime(finishTime) {}
    friend bool operator < (information A, information B)
    {
        return A.finishTime > B.finishTime;
    }
};
 
vector <node> G[maxn], G_REV[maxn], graph[maxn];
 
bool vis[maxn];
int dfsTime;
int dis[maxn];
int finish[maxn];
 
inline void dfs(int u)
{
    dis[u] = ++dfsTime;
    for (node e : G[u])
    {
        if (vis[e.v]) continue;
        vis[e.v] = 1;
        dfs(e.v);
    }
    finish[u] = dfsTime++;
}
 
set <int> scc;
 
inline void dfs2(int u)
{
    scc.insert(u);
    for (node e : G_REV[u])
    {
        if (vis[e.v]) continue;
        vis[e.v] = 1;
        dfs2(e.v);
    }
}
 
inline void initialize()
{
    dfsTime = 0;
    scc.clear();
    for (int i = 0; i < maxn; i++)
    {
        graph[i].clear();
        dis[i] = 0;
        finish[i] = 0;
    }
}
 
int dp[maxn][maxn];
 
inline double karps()
{
    int tNode = scc.size();
    int u;
    for (auto it : scc)
    {
        u = it;
        break;
    }
    int here[maxn] = {0};
    for (int n : scc) here[n] = 1;
    for (int n : scc)
    {
        for (node e : G[n])
        {
            if (!here[e.v]) continue;
            graph[n].push_back(e);
        }
    }
    for (int i = 0; i < maxn; i++)
    {
        for (int j = 0; j < maxn; j++)
        {
            dp[i][j] = inf;
        }
    }
    dp[0][u] = 0;
    for (int k = 1; k <= tNode; k++)       // edge
    {
        for (int u : scc)                  // node num
        {
            for (node e : graph[u])        // child
            {
                if (dp[k - 1][u] != inf)
                {
                    int wt = dp[k - 1][u] + e.w;
                    dp[k][e.v] = min(dp[k][e.v], wt);
                }
            }
        }
    }
    double mean[maxn];
    for (int i = 0; i < maxn; i++) mean[i] = -1;
    for (int u : scc)
    {
        int fCost = dp[tNode][u];
        if (fCost == inf) continue;
        for (int k = 0; k < tNode; k++)
        {
            int nowCost = dp[k][u];
            if (nowCost != inf && nowCost <= fCost)
            {
                double m = ((double)fCost - nowCost) / (tNode - k);
                mean[u] = max(mean[u], m);    // take maximum
            }
        }
    }
    double res;
    res = inf;
    for (int u : scc)
    {
        if (mean[u] == -1) continue;
        res = min(res, mean[u]);
    }
    return res;
}
 
int main()
{
    // FI;
    FAST;

    int tc;
    cin >> tc;
    for (int tcase = 1; tcase <= tc; tcase++)
    {
        int tNode, tEdge;
        cin >> tNode >> tEdge;
        for (int e = 0; e < tEdge; e++)
        {
            int u, v, w;
            cin >> u >> v >> w;
            G[u].push_back({v, w});        // original graph
            G_REV[v].push_back({u, w});    // reverse graph
        }
        for (int i = 0; i < maxn; i++) vis[i] = 0;
        for (int i = 1; i <= tNode; i++) if (!vis[i]) dfs(i);
        vector <information> data;
        for (int i = 1; i <= tNode; i++) data.push_back({i, finish[i]});
        Sort(data);
        for (int i = 0; i < maxn; i++) vis[i] = 0;
        double res = inf;
        bool exist = false;
        for (information e : data)
        {
            if (vis[e.node]) continue;
            initialize();
            dfs2(e.node);
            res = min(res, karps());
        }
        cout << "Case #" << tcase << ": ";
        if (res == inf) cout << "No cycle found." << endl;
        else cout << fixed << setprecision(2) << res << endl;
        for (int i = 0; i < maxn; i++)
        {
            G[i].clear();
            G_REV[i].clear();
        }
    }
}
 
/****************************************
1
4 6
1 2 1
1 3 10
2 3 3
3 4 2
4 2 0
4 1 8
 
1.67
*****************************************/
 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.