poj 1570 floyd

这题过的人很少,却意外的很水呢

#include <iostream>
#include <string>
#include <map>
using namespace std;
char ch;
int k1,k2,a[201][201][2],tot,g;
string s,ss;
map<string,int> ma;
int gcd(int a,int b) {
    if (b==0) return a;
    else return gcd(b,a%b);
}
int main() {
    tot = 0;
    ma.clear();
    while (scanf("%c",&ch)) {
          if (ch=='.') break;
          else if (ch=='!') {
               scanf("%d",&k1);
               cin >> s;
               if (ma[s]==0) ma[s] = ++tot;
               scanf(" = %d",&k2);
               cin >> ss;
               if (ma[ss]==0) ma[ss] = ++tot;
               a[ma[s]][ma[ss]][0] = k1;
               a[ma[s]][ma[ss]][1] = k2;
               a[ma[ss]][ma[s]][0] = k2;
               a[ma[ss]][ma[s]][1] = k1;
               for (int k=1;k<=tot;k++)
                   for (int i=1;i<=tot;i++)
                       for (int j=1;j<=tot;j++)
                           if (i==j) continue;
                           else if (a[i][j][0]==0 && a[i][k][0]!=0 && a[k][j][0]!=0) {
                                a[i][j][0] = a[i][k][0]*a[k][j][0];
                                a[i][j][1] = a[i][k][1]*a[k][j][1];
                           }
          }
          else {
               cin >> s;
               scanf(" = ");
               cin >> ss;
               k1 = ma[s];
               k2 = ma[ss];
               if (a[k1][k2][0]==0) cout << "? " << s << " = ? " << ss << endl;
               else {
                    g = gcd(a[k1][k2][0],a[k1][k2][1]);
                    cout << a[k1][k2][0]/g << ' ' << s << " = " << a[k1][k2][1]/g << ' ' << ss << endl;
               }
          }
          getchar();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值