Problem Link
এটা modified shortest path finding algorithm(BFS/DFS/Dijkstra/Floyed Warshall যেকোন একটা) দিয়ে করতে হবে। যেহেতু input এর range অনেক ছোট তাই আমি Floyed Warshall দিয়ে করেছি।
My Code:
#include<bits/stdc++.h>
using namespace std;
#define inf 100005
int n, r;
int d[205][205];
void FloyedWarshall()
{
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
d[i][j] = d[j][i] = max(d[i][j],min(d[i][k],d[k][j]));
}
int main()
{
for(int t = 1; scanf("%d %d",&n,&r) && (n!=0 && r!=0); t++)
{
map<string,int>name;
string u, v, Start, End;
int cost, idx = 1;
for(int i = 0; i < 100; i++)
for(int j = 0; j < 100; j++)
if(i==j) d[i][j] = 0;
else d[i][j] = -inf;
for(int i = 0; i < r; i++)
{
cin>>u>>v>>cost;
if(!name[u])
name[u] = idx++;
if(!name[v])
name[v] = idx++;
d[name[u]][name[v]] = cost;
d[name[v]][name[u]] = cost;
}
FloyedWarshall();
cin>>Start>>End;
cout<<"Scenario #"<<t<<endl;
cout<<d[name[Start]][name[End]]<<" tons"<<endl;
cout<<endl;
name.clear();
}
return 0;
}
এটা modified shortest path finding algorithm(BFS/DFS/Dijkstra/Floyed Warshall যেকোন একটা) দিয়ে করতে হবে। যেহেতু input এর range অনেক ছোট তাই আমি Floyed Warshall দিয়ে করেছি।
My Code:
#include<bits/stdc++.h>
using namespace std;
#define inf 100005
int n, r;
int d[205][205];
void FloyedWarshall()
{
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
d[i][j] = d[j][i] = max(d[i][j],min(d[i][k],d[k][j]));
}
int main()
{
for(int t = 1; scanf("%d %d",&n,&r) && (n!=0 && r!=0); t++)
{
map<string,int>name;
string u, v, Start, End;
int cost, idx = 1;
for(int i = 0; i < 100; i++)
for(int j = 0; j < 100; j++)
if(i==j) d[i][j] = 0;
else d[i][j] = -inf;
for(int i = 0; i < r; i++)
{
cin>>u>>v>>cost;
if(!name[u])
name[u] = idx++;
if(!name[v])
name[v] = idx++;
d[name[u]][name[v]] = cost;
d[name[v]][name[u]] = cost;
}
FloyedWarshall();
cin>>Start>>End;
cout<<"Scenario #"<<t<<endl;
cout<<d[name[Start]][name[End]]<<" tons"<<endl;
cout<<endl;
name.clear();
}
return 0;
}
No comments:
Post a Comment