Skip to content

Instantly share code, notes, and snippets.

@msg555
Created November 7, 2012 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msg555/4029362 to your computer and use it in GitHub Desktop.
Save msg555/4029362 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
double find(long long a, long long b, long long c, long long &d){
if(!b||!c)return -1;
d=ceil((double) a* (double) c /(double) b);
if(d>100000)return -1;
return (double) d* (double) b /(double)c;
};
int main(){
int n;
long long a1,a2;
string s1,s2,c;
cin >> n;
int tt=0;
while(n){
int cur=0;
long long a[10][10];
string s[10];
int n1,n2;
for(int i=0;i<n;i++){
cin>>a1>>s1>>c>>a2>>s2;
n1=-1;n2=-1;
for(int j=0;j<cur;j++){
if(s[j]==s1)n1=j;
if(s[j]==s2)n2=j;
};
if(n1==-1){
s[cur]=s1;n1=cur;
cur++;
};
if(n2==-1){
s[cur]=s2;n2=cur;
cur++;
}
a[n1][n2]=a1;
a[n2][n1]=a2;
};
for(int k=0;k<cur;k++)
for(int i=0;i<cur;i++)
for(int j=0;j<cur;j++)
if(a[i][k]&&a[k][j]&&!a[i][j]){
a[i][j]=a[i][k]*a[k][j];
a[j][i]=a[k][i]*a[j][k];
};
long long amount,mina,d;
string name,namemin;
cin>>amount>>name;
int nn;
for(int i=0;i<cur;i++)
if(s[i]==name)nn=i;
double min=1e100;
for(int i=0;i<cur;i++){
double am=find(amount,a[nn][i],a[i][nn],d);
if(i!=nn&&am>0&&am<min){min=am;namemin=s[i];mina=d;};
};
tt++;
cout<<"Case "<<tt<<": "<<mina<<' '<<namemin<<endl;
cin>>n;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment