【t033】单位unit

来源:互联网 发布:seo推广渠道 编辑:程序博客网 时间:2024/06/09 21:52

Time Limit: 1 second
Memory Limit: 64 MB

【问题描述】

某星球上有很多计量系统,之间的计量单位的转换很繁琐。希望你能编程解决这个问题。
现有N (1 <= N <= 100)个转换关系(比如:12 A 等于 1 B, 3 B等于 1 C等等 ),请计算指定的M (1 <= M <= 100) 个转换关系(比如:多少 A 等于 1 C)。

【输入格式】

第一行:两个整数 N 和 M。
下面N行:每行有3个值:A S1 S2,A是一个小数,S1、S2是两个单词,它们之间用空格分开。表示 A 个 S1 等于 1个S2。
再后面有M行:每行两个单词X Y,中间用空格分开,请你计算出1个Y相当于多少X。
【输出格式】

共M行:每行对应一个输入数据中的单位转换问题,答案为乘1000之后四舍五入取整。保证答案不超过2^31。

Sample Input

4 1
12 inch foot
3 foot yard
5280 foot mile
0.0254 meter inch
meter mile

Sample Output

1609344

【题目链接】:http://noi.qz5z.com/viewtask.asp?id=t033

【题意】

【题解】

这题的题目数据不知道怎么回事,不能让C++通过;
自己手动测是正确的;
可以转换成图论的模型吧;
对于
t x y
则建立一条从x指向y的有向边,边权为t;
同时建立一条从y指向x的有向边,边权为1/t;
w[x][y]就表示1个y可以换成几个x;
然后用floyd搞出任意两个之间的(可能不能全部)的换算就好了;
可以加个bo数组判断某两个单位之间之前有没有已经知道;已经知道就不要换算了;
这里不是加法而是乘法了;
round()是四舍五入函数;

【完整代码】

#include <cstdio>#include <cmath>#include <iostream>#include <algorithm>#include <string>#include <map>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define LL long long#define rep1(i,a,b) for (int i = a;i <= b;i++)#define rep2(i,a,b) for (int i = a;i >= b;i--)#define mp make_pair#define pb push_back#define fi first#define se second#define rei(x) scanf("%d",&x)#define rel(x) scanf("%I64d",&x)typedef pair<int,int> pii;typedef pair<LL,LL> pll;const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};const double pi = acos(-1.0);const int N = 210;int n,m,tot;map <string,int> dic;double w[N][N],t;bool bo[N][N] = {0};string s1,s2;int main(){    //freopen("F:\\rush.txt","r",stdin);    rei(n);rei(m);    rep1(i,1,n)    {        cin >> t >> s1 >> s2;        int x,y;        if (!dic[s1]) dic[s1]=++tot;        if (!dic[s2]) dic[s2]=++tot;        x = dic[s1],y = dic[s2];        w[x][y] = t,w[y][x] = 1/t;        bo[x][y] = true,bo[y][x] = true;    }    rep1(k,1,tot)        rep1(i,1,tot)            rep1(j,1,tot)                if (i!=j && i!=k && j!=k)                {                    if (bo[i][j]) continue;                    if (!bo[i][k] || !bo[k][j]) continue;                    w[i][j] = w[i][k]*w[k][j];                    bo[i][j] = true;                }    rep1(i,1,m)    {        cin >> s1 >> s2;        int x = dic[s1],y = dic[s2];        double d = w[x][y]*1000;        int t = round(d);        cout << t << endl;    }    return 0;}
0 0