Uva 721 - Invitation Cards
Problem Statement: 721 - Invitation Cards
/**
* @Author: Pranta Sarker
*
**/
// Algo: Dijkstra
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define pfn(x , k) printf(k , x)
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
#define clr clear()
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const LL high = 1e6+5;
LL minimum_cost = 0 , dis[high], visited[high];
struct data
{
int node, cost;
data(){ }
data(int n , int c)
{
node = n;
cost = c;
}
bool operator<(const data &b) const
{
return cost > b.cost;
}
};
vector<data>adj[high] , back_adj[high];
void CLR(int n)
{
for(int i=1; i<=n; i++)
{
adj[i].clr;
back_adj[i].clr;
visited[i] = 0;
}
}
void Dijkstra(int s, int n, vector<data>adj[])
{
LL i=0 , j=0 , u=0 , v = 0 , w = 0, cost = 0;
for(i=1; i<=n; i++) dis[i] = 1e15;
dis[s] = 0;
priority_queue<data>Q;
Q.push(data(s , 0));
while(!Q.empty())
{
u = Q.top().node;
w = Q.top().cost;
//cout << "u = " << u << " w = " << w << "\n";
Q.pop();
visited[u] = 0;
for(i=0; i<adj[u].size(); i++)
{
v = adj[u][i].node;
//cout << "V = " << v << "; ";
int tm = dis[u];
//cout << "tm = " << tm << "\n";
//cost = adj[u][i].cost + w; // not the current state weight
//cout << "1.cost = " << cost << "\n";
cost = adj[u][i].cost + tm; // take always minimum state weight for each u
//cout << "2.cost = " << cost << "\n";
if(dis[v] > cost)
{
dis[v] = cost;
//cout << "\ndis[" << v << "] = " << dis[v] << "\n";
if(!visited[v])
{
Q.push(data(v , cost));
visited[v] = 1;
}
}
}
}
for(i=1; i<=n; i++)
{
minimum_cost += dis[i];
}
}
int main()
{
fast;
int N;
cin >> N;
while(N--)
{
minimum_cost = 0;
int u , v , w , i , P , Q;
cin >> P >> Q;
CLR(P);
for(i=0; i<Q; i++)
{
cin >> u >> v >> w;
adj[u].psb(data(v , w));
back_adj[v].psb(data(u , w));
}
Dijkstra(1, P, adj);
//cout << "2.\n";
Dijkstra(1, P, back_adj);
cout << minimum_cost << "\n";
}
return 0;
}
/**
* @Author: Pranta Sarker
*
**/
// Algo: Dijkstra
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define pfn(x , k) printf(k , x)
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back
#define clr clear()
typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;
const int mod = 1000007;
const LL high = 1e6+5;
LL minimum_cost = 0 , dis[high], visited[high];
struct data
{
int node, cost;
data(){ }
data(int n , int c)
{
node = n;
cost = c;
}
bool operator<(const data &b) const
{
return cost > b.cost;
}
};
vector<data>adj[high] , back_adj[high];
void CLR(int n)
{
for(int i=1; i<=n; i++)
{
adj[i].clr;
back_adj[i].clr;
visited[i] = 0;
}
}
void Dijkstra(int s, int n, vector<data>adj[])
{
LL i=0 , j=0 , u=0 , v = 0 , w = 0, cost = 0;
for(i=1; i<=n; i++) dis[i] = 1e15;
dis[s] = 0;
priority_queue<data>Q;
Q.push(data(s , 0));
while(!Q.empty())
{
u = Q.top().node;
w = Q.top().cost;
//cout << "u = " << u << " w = " << w << "\n";
Q.pop();
visited[u] = 0;
for(i=0; i<adj[u].size(); i++)
{
v = adj[u][i].node;
//cout << "V = " << v << "; ";
int tm = dis[u];
//cout << "tm = " << tm << "\n";
//cost = adj[u][i].cost + w; // not the current state weight
//cout << "1.cost = " << cost << "\n";
cost = adj[u][i].cost + tm; // take always minimum state weight for each u
//cout << "2.cost = " << cost << "\n";
if(dis[v] > cost)
{
dis[v] = cost;
//cout << "\ndis[" << v << "] = " << dis[v] << "\n";
if(!visited[v])
{
Q.push(data(v , cost));
visited[v] = 1;
}
}
}
}
for(i=1; i<=n; i++)
{
minimum_cost += dis[i];
}
}
int main()
{
fast;
int N;
cin >> N;
while(N--)
{
minimum_cost = 0;
int u , v , w , i , P , Q;
cin >> P >> Q;
CLR(P);
for(i=0; i<Q; i++)
{
cin >> u >> v >> w;
adj[u].psb(data(v , w));
back_adj[v].psb(data(u , w));
}
Dijkstra(1, P, adj);
//cout << "2.\n";
Dijkstra(1, P, back_adj);
cout << minimum_cost << "\n";
}
return 0;
}
Comments
Post a Comment