Showing posts with label spfa. Show all posts
Showing posts with label spfa. Show all posts

Tuesday, April 30, 2019

[Gym] L. The Shortest Path

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : L. The Shortest Path
Source            : Codeforces Gym
Category          : Graph Theory
Algorithm         : spfa, Find Negative Cycle 
Verdict           : Accepted 

  1. #include "bits/stdc++.h"  
  2.   
  3. using namespace std;  
  4.   
  5. #define ll                  long long int  
  6.   
  7. static const int maxn = 2e3 + 5;  
  8. static const ll inf = (ll)1e18;  
  9.   
  10. struct Node  
  11. {  
  12.       int v;  
  13.       ll w;  
  14.       Node() {}  
  15.       Node(int v = 0, ll w = 0) : v(v), w(w) {}  
  16. };  
  17.   
  18. vector <Node> graph[maxn];  
  19. int tnode, tedge;  
  20. ll dist[maxn], cnt[maxn];  
  21. bool inqueue[maxn];  
  22.   
  23. pair <ll, bool> spfa()  
  24. {  
  25.       queue <int> PQ;  
  26.       for (int i = 1; i <= tnode; i++)  
  27.       {  
  28.             PQ.push(i);  
  29.             dist[i] = 0;  
  30.             inqueue[i] = 1;  
  31.             cnt[i] = 1;  
  32.       }  
  33.       while (!PQ.empty())  
  34.       {  
  35.             int u = PQ.front(); PQ.pop();  
  36.             inqueue[u] = 0;  
  37.             for (Node p : graph[u])  
  38.             {  
  39.                   int v = p.v;  
  40.                   ll w = p.w;  
  41.                   if (dist[u] + w < dist[v])  
  42.                   {  
  43.                         dist[v] = dist[u] + w;  
  44.                         if (!inqueue[v])  
  45.                         {  
  46.                               cnt[v]++;  
  47.                               inqueue[v] = 1;  
  48.                               PQ.push(v);  
  49.                         }  
  50.                         if (cnt[u] > tnode) return make_pair(0, false); // Negative Cycle found  
  51.                   }  
  52.             }  
  53.       }  
  54.       ll mini = inf;  
  55.       for (int i = 1; i <= tnode; i++) mini = min(mini, dist[i]);  
  56.       return make_pair(mini, true);  
  57. }  
  58.   
  59.   
  60. int main()  
  61. {  
  62.       ios_base::sync_with_stdio(false);  
  63.       cin.tie(nullptr);  
  64.       cout.tie(nullptr);  
  65.       cout << fixed << setprecision(15);  
  66.       #ifndef ONLINE_JUDGE  
  67.             freopen("in.txt", "r", stdin);  
  68.             // freopen("out.txt", "w", stdout);  
  69.       #endif // ONLINE_JUDGE  
  70.   
  71.       int tc;  
  72.       cin >> tc;  
  73.       for (int tcase = 1; tcase <= tc; tcase++)  
  74.       {  
  75.             for (int i = 1; i < maxn; i++) graph[i].clear();  
  76.             cin >> tnode >> tedge;  
  77.             ll minw = inf;  
  78.             for (int e = 1; e <= tedge; e++)  
  79.             {  
  80.                   int u, v;  
  81.                   ll w;  
  82.                   cin >> u >> v >> w;  
  83.                   graph[u].push_back({v, w});  
  84.                   minw = min(minw, w);  
  85.             }  
  86.             if (minw >= 0)  
  87.             {  
  88.                   cout << minw << '\n';  
  89.                   continue;  
  90.             }  
  91.             pair <ll, bool> ans = spfa();  
  92.             if (ans.second == false) cout << "-inf\n";  
  93.             else cout << ans.first << '\n';  
  94.       }  
  95. }  

Sunday, January 6, 2019

[Spoj] Wandering Queen

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : Wandering Queen
Source            : Spoj
Category          : Graph Theory
Algorithm         : spfa
Verdict           : Accepted 
#include "bits/stdc++.h"
 
using namespace std;
 
#define FOR(i, n)                 for (int i = 1; i <= n; i++)
#define For(i, n)                 for (size_t i = 0; i < n; i++)
 
#define inf                       12345678
 
static const int maxn = 1000 + 5;
 
int dr[] = {0, 0, 1, -1, 1, 1, -1, -1};  // 8 Direction
int dc[] = {1, -1, 0, 0, 1, -1, 1, -1};
 
struct info
{
    int x, y;
    info(int x = 0, int y = 0) : x(x), y(y) {}
};
 
int row, column;
int dis[maxn][maxn];
bool now[maxn][maxn];
char grid[maxn][maxn];
 
bool inside(int r, int c)
{
    return r >= 1 && r <= row && c >= 1 && c <= column;
}
 
int spfa(info src, info des)
{
    FOR(i, row) FOR(j, column) dis[i][j] = inf, now[i][j] = 0;
    queue <info> PQ;
    PQ.emplace(src);
    dis[src.x][src.y] = 0;
    now[src.x][src.y] = 1;
    while(!PQ.empty())
    {
        info u = PQ.front(); PQ.pop();
        now[u.x][u.y] = 0;
        For(i, 8)
        {
            bool push = false;
            int prex = u.x, prey = u.y;
            while (true)
            {
                int x = prex + dr[i];
                int y = prey + dc[i];
                if (!inside(x, y) || grid[x][y] == 'X') break;
                if (dis[x][y] >= dis[u.x][u.y] + 1)
                {
                    dis[x][y] = dis[u.x][u.y] + 1;
                    if (!now[x][y]) now[x][y] = 1, PQ.emplace(x, y);
                    prex = x, prey = y;
                }
                else break;
            }
        }
    }
    if (dis[des.x][des.y] == inf) return -1;
    return dis[des.x][des.y];
}
 
int main()
{
    //freopen("in.txt", "r", stdin);
 
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 
    int tc;
    cin >> tc;
    FOR(tcase, tc)
    {
        cin >> row >> column;
        info s, t;
        FOR(i, row)
        {
            FOR(j, column)
            {
                cin >> grid[i][j];
                if (grid[i][j] == 'S') s.x = i, s.y = j;
                if (grid[i][j] == 'F') t.x = i, t.y = j;
            }
        }
        int ans = spfa(s, t);
        cout << ans << endl;
    }
} 

Friday, December 14, 2018

[UVa] 13010 - Galactic taxes

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : 13010 - Galactic taxes
Source            : UVa Online Judge
Category          : Graph Theory, Search
Algorithm         : spfa, Ternary Search
Verdict           : Accepted  
  1. #include <bits/stdc++.h>  
  2.    
  3. using namespace std;  
  4.    
  5. #define inf             1e13  
  6.    
  7. static const int maxn = 1000 + 5;  
  8.    
  9. struct node  
  10. {  
  11.     int v, A, B;  
  12.     node() {}  
  13.     node(int v, int A, int B)  
  14.     {  
  15.         this->v = v;  
  16.         this->A = A;  
  17.         this->B = B;  
  18.     }  
  19. };  
  20.    
  21. vector <node> g[maxn];  
  22.    
  23. int N, M;  
  24. double dist[maxn];  
  25. bool inQueue[maxn];  
  26.    
  27. double SPFA(double t)  
  28. {  
  29.     for (int i = 1; i < maxn; i++)  
  30.     {  
  31.         dist[i] = inf;  
  32.         inQueue[i] = 0;  
  33.     }  
  34.     queue <int> PQ;  
  35.     PQ.push(1);  
  36.     dist[1] = 0.0;  
  37.     inQueue[1] = 1;  
  38.     while (!PQ.empty())  
  39.     {  
  40.         int u = PQ.front(); PQ.pop();  
  41.         inQueue[u] = 0;  
  42.         for (auto &it : g[u])  
  43.         {  
  44.             double cost = (double)it.A * t + (double)it.B;  
  45.             int v = it.v;  
  46.             if (dist[v] > dist[u] + cost)  
  47.             {  
  48.                 dist[v] = dist[u] + cost;  
  49.                 if (!inQueue[v])  
  50.                 {  
  51.                     inQueue[v] = 1;  
  52.                     PQ.push(v);  
  53.                 }  
  54.             }  
  55.         }  
  56.     }  
  57.     return dist[N];  
  58. }  
  59.    
  60. void ternary_search()  
  61. {  
  62.     double low = 0.0;  
  63.     double high = 1440.0;  
  64.     double ans = -inf;  
  65.     for (int tol = 0; tol < 300; tol++)  
  66.     {  
  67.         double mid1 = low + (high - low) / 3.0;  
  68.         double mid2 = high - (high - low) / 3.0;  
  69.         double fmid1 = SPFA(mid1);  
  70.         double fmid2 = SPFA(mid2);  
  71.         ans = max(ans, max(fmid1, fmid2));  
  72.         if (fmid1 < fmid2) low = mid1;  
  73.         else high = mid2;  
  74.     }  
  75.     cout << fixed << setprecision(5) << ans << endl;  
  76. }  
  77.    
  78. int main()  
  79. {  
  80.     //freopen("in.txt", "r", stdin);  
  81.    
  82.     while (cin >> N >> M)  
  83.     {  
  84.         for (int i = 0; i < M; i++)  
  85.         {  
  86.             int u, v, A, B;  
  87.             cin >> u >> v >> A >> B;  
  88.             g[u].push_back({v, A, B});  
  89.             g[v].push_back({u, A, B});  
  90.         }  
  91.         ternary_search();  
  92.         for (int i = 0; i < maxn; i++) g[i].clear();  
  93.     }  
  94. }  
  95.