#include <bits/stdc++.h>
#define mem(n) memset(n, 0, sizeof(n))
using namespace std;
vector <int> store[33];
int vis[33], level[33];
void BFS(int src, int dest, int tot){
queue <int> Q;
Q.push(src);
vis[src] = 1;
while(!Q.empty()){
int u = Q.front();
Q.pop();
int sz = store[u].size();
for(int i = 0; i < sz; i++){
int v = store[u][i];
if(!vis[v]){
vis[v] = 1;
Q.push(v);
level[v] = level[u]+1;
}
}
}
if(!level[dest])cout << "NO SHIPMENT POSSIBLE" << endl;
else cout << "$" << level[dest]*tot << endl;
}
int main(){
int node, edge, shipment;
int res, ret, tmp, test, cs = 0;
string start, dest, nde;
map <string, int> M;
cin >> test;
puts("SHIPPING ROUTES OUTPUT");
puts("");
while(test--){
cin >> node >> edge >> shipment;
for(int i = 1; i <= node; i++)cin >> nde, M[nde] = i;
for(int i = 1; i <= edge; i++){
cin >> start >> dest;
store[M[start]].push_back(M[dest]);
store[M[dest]].push_back(M[start]);
}
cout << "DATA SET " << ++cs << endl << endl;
for(int i = 1; i <= shipment; i++){
mem(level), mem(vis);
cin >> tmp >> start >> dest;
BFS(M[start], M[dest], tmp*100);
}
M.clear();
for(int i = 1; i <= node; i++)store[i].clear();
puts("");
}
puts("END OF OUTPUT");
return 0;
}
#define mem(n) memset(n, 0, sizeof(n))
using namespace std;
vector <int> store[33];
int vis[33], level[33];
void BFS(int src, int dest, int tot){
queue <int> Q;
Q.push(src);
vis[src] = 1;
while(!Q.empty()){
int u = Q.front();
Q.pop();
int sz = store[u].size();
for(int i = 0; i < sz; i++){
int v = store[u][i];
if(!vis[v]){
vis[v] = 1;
Q.push(v);
level[v] = level[u]+1;
}
}
}
if(!level[dest])cout << "NO SHIPMENT POSSIBLE" << endl;
else cout << "$" << level[dest]*tot << endl;
}
int main(){
int node, edge, shipment;
int res, ret, tmp, test, cs = 0;
string start, dest, nde;
map <string, int> M;
cin >> test;
puts("SHIPPING ROUTES OUTPUT");
puts("");
while(test--){
cin >> node >> edge >> shipment;
for(int i = 1; i <= node; i++)cin >> nde, M[nde] = i;
for(int i = 1; i <= edge; i++){
cin >> start >> dest;
store[M[start]].push_back(M[dest]);
store[M[dest]].push_back(M[start]);
}
cout << "DATA SET " << ++cs << endl << endl;
for(int i = 1; i <= shipment; i++){
mem(level), mem(vis);
cin >> tmp >> start >> dest;
BFS(M[start], M[dest], tmp*100);
}
M.clear();
for(int i = 1; i <= node; i++)store[i].clear();
puts("");
}
puts("END OF OUTPUT");
return 0;
}
Love your code...I admire your coding/solving design and implementation of code...so nice. loved it.
ReplyDelete