Uva 10986 - Sending email
/**
* @Author: Pranta Sarker
*
**/
// Dijkstra
// Accepted
#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()
#define inf 1000000000
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 int high = 20005;
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];
int visited[high];
int dis[high];
void CLR()
{
for(int i=0; i<high; i++)
{
adj[i].clr;
visited[i] = 0;
}
}
int Dijkstra(int s, int term)
{
int w = 0 , u=0, v=0, i=0 , cost = 0;
for(int i=0; i<high; i++) dis[i] = inf;
dis[s] = 0;
priority_queue<data>Q;
Q.push(data(s , 0));
while(!Q.empty())
{
u = Q.top().node;
Q.pop();
visited[u] = 0;
for(i=0; i<adj[u].size(); i++)
{
v = adj[u][i].node;
cost = adj[u][i].cost + dis[u];
if(dis[v] > cost)
{
dis[v] = cost;
if(!visited[v])
{
Q.push(data(v , cost));
visited[v] = 1;
}
}
}
}
return dis[term];
}
int main()
{
fast;
int test, caseno=0;
cin >> test;
while(test--)
{
CLR();
int x , y , w , i , n , m , S , T;
cin >> n >> m >> S >> T;
for(i=0; i<m; i++)
{
cin >> x >> y >> w;
adj[x].psb(data(y , w));
adj[y].psb(data(x , w));
}
int ans = Dijkstra(S, T);
cout << "Case #" << ++caseno << ": ";
if(ans != inf) cout << ans << "\n";
else cout << "unreachable\n";
// for(i=0; i<n; i++)
// {
// cout << dis[i] << "; ";
// }
}
return 0;
}
* @Author: Pranta Sarker
*
**/
// Dijkstra
// Accepted
#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()
#define inf 1000000000
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 int high = 20005;
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];
int visited[high];
int dis[high];
void CLR()
{
for(int i=0; i<high; i++)
{
adj[i].clr;
visited[i] = 0;
}
}
int Dijkstra(int s, int term)
{
int w = 0 , u=0, v=0, i=0 , cost = 0;
for(int i=0; i<high; i++) dis[i] = inf;
dis[s] = 0;
priority_queue<data>Q;
Q.push(data(s , 0));
while(!Q.empty())
{
u = Q.top().node;
Q.pop();
visited[u] = 0;
for(i=0; i<adj[u].size(); i++)
{
v = adj[u][i].node;
cost = adj[u][i].cost + dis[u];
if(dis[v] > cost)
{
dis[v] = cost;
if(!visited[v])
{
Q.push(data(v , cost));
visited[v] = 1;
}
}
}
}
return dis[term];
}
int main()
{
fast;
int test, caseno=0;
cin >> test;
while(test--)
{
CLR();
int x , y , w , i , n , m , S , T;
cin >> n >> m >> S >> T;
for(i=0; i<m; i++)
{
cin >> x >> y >> w;
adj[x].psb(data(y , w));
adj[y].psb(data(x , w));
}
int ans = Dijkstra(S, T);
cout << "Case #" << ++caseno << ": ";
if(ans != inf) cout << ans << "\n";
else cout << "unreachable\n";
// for(i=0; i<n; i++)
// {
// cout << dis[i] << "; ";
// }
}
return 0;
}
Comments
Post a Comment