Problem K: Yikes -- Bikes!

http://acm.upc.edu.cn/problem.php?id=2780

昨天做的题,没过……!!!伤心……
题意:给你n个单位,n-1组关系,让你单位换算……
解题思路:Floyd算法
自己听别人说用Floyd算法,然后自己默默的用有向图写……但是!!!Floyd算法不能用有向图……!所以只能在其相反的转化中标记为负的,在进行时特殊处理一下,最后便利找出能进行单位转化的那组单位,然后进行大小排序,最后就莫名其妙的哦过了……!!!

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <cctype>
#include <set>
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())

typedef long long LL;

using namespace std;

int graph[30][30];
int c[30];
int topo[30];
int n;
int t;
struct Value{
    int x,y;
};

bool cmp(Value a,Value b){
    return a.x > b.x;
}

int main()
{
#ifndef ONLINE_JUDGE
   freopen("in.in","r",stdin);
#endif
    map<string,int>un;
    string name[30];
    while(cin >> n && n){
    for(int i = 1;i <= n;i++){
        cin >> name[i];
        un[ name[i] ] = i;
    }
    memset(graph,0,sizeof(graph));
    for(int i = 1;i <= n-1;i++){
        string str1;
        int num,a,b;
        cin >> str1;
        a = un[str1];
        cin >> str1 >> num >> str1;
        b = un[str1];
        graph[a][b] = num;
        graph[b][a] = -1 *num;
        //cout <<a << " " << b << endl;
    }
    for(int k = 1;k <= n;k++){
        for(int i = 1;i <= n;i++){
            for(int j = 1;j <= n;j++){
                if(i == j || k == i || k == j)
                    continue;
                if(!graph[i][j] &&graph[i][k] &&graph[k][j]){
                    if(graph[i][k] > 0 && graph[k][j] > 0){
                        graph[i][j] = graph[i][k] * graph[k][j];
                        graph[j][i] = -1 * graph[i][k] * graph[k][j];
                    }
                    else if(graph[i][k] < 0 && graph[k][j] < 0){
                        graph[j][i] = graph[i][k] * graph[k][j];
                        graph[i][j] = -1 * graph[i][k] * graph[k][j];
                    }
                    else if(graph[i][k] < 0 && graph[k][j] > 0){
                        if(abs(graph[i][k]) > graph[k][j]){
                            graph[j][i] = abs(graph[i][k]) / graph[k][j];
                            graph[i][j] = -1 * graph[j][i];
                        }
                        else{
                            graph[i][j] = abs(graph[i][k]) / graph[k][j];
                            graph[j][i] = -1 * graph[i][j];
                        }
                    }
                    else{
                        if(graph[i][k] > abs(graph[k][j])){
                            graph[i][j] = graph[i][k] / abs(graph[k][j]);
                            graph[j][i] = -1 * graph[i][j];
                        }
                        else{
                            graph[j][i] = graph[i][k] / abs(graph[k][j]);
                            graph[i][j] = -1 * graph[j][i];
                        }
                    }
                }
            }
        }
    }
    int mark;
    for(int i = 1;i <= n;i++){
        bool flag = 1;;
        for(int j = 1;j <= n;j++){
            if(graph[i][j] < 0){
                flag = 0;
                break;
            }
        }
        if(flag){
            mark = i;
            break;
        }
    }
    Value a[30];
    for(int i = 1;i <= n;i++){
        a[i-1].x = graph[mark][i];
        a[i-1].y = i;
    }
    sort(a,a+n,cmp);
    cout << 1  << name[ a[n-1].y ];
    for(int i = n-2;i > -1;i--){
        cout << " = "<< a[i].x << name[ a[i].y ] ;
    }
    cout << endl;
    }
    return 0;
}

时间: 2024-12-22 11:19:00

Problem K: Yikes -- Bikes!的相关文章

Moscow Subregional 2010 Problem K. KMC Attacks 交互题、队列优化、枚举

ACM ICPC 2010-2011 NEERC Moscow Subregional Contest Moscow, October 24, 2010 Problem K. KMC Attacks Time limit: 2 seconds Memory limit: 256 megabytes Warrant VI is a remote planet located in the Koprulu Sector. Warrant VI features a strange huge fiel

Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100610 Description Robots are becoming more and more popular. They are used nowadays not only in manufacturing plants, but also at home. One programmer wit

XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem K. Piecemaking

题目:Problem K. PiecemakingInput file: standard inputOutput file: standard outputTime limit: 1 secondMemory limit: 512 mebibytesThe civil war in Berland continues for five years already. The United Nation decided to end the bloodshed.Berland consists o

华农oj Problem K: 负2进制【有技巧构造/待补】

Problem K: 负2进制 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 51 Solved: 6 [Submit][Status][Web Board] Description 如果我16岁,我可以悄悄的说我好喜欢你:如果我26岁,我可以大声告诉你我很爱你:可惜我6岁,我什么都给不了你,我还要上小学. 我们都知道2进制,每一位的权值如下: 1 2 4 8 16 32 64 现在我们定义一种-2进制,每一位的权值如下: 1 -2 4 -8 16

2018 Multi-University Training Contest 4 Problem K. Expression in Memories 【模拟】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6342 Problem K. Expression in Memories Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2150    Accepted Submission(s): 772Special Judge Problem De

UESTC_Sliding Window 2015 UESTC Training for Data Structures&lt;Problem K&gt;

K - Sliding Window Time Limit: 18000/6000MS (Java/Others)     Memory Limit: 131072/131072KB (Java/Others) Submit Status An array of size n≤106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the

UESTC_摩天轮 2015 UESTC Training for Dynamic Programming&lt;Problem K&gt;

K - 摩天轮 Time Limit: 10000/4000MS (Java/Others)     Memory Limit: 262143/262143KB (Java/Others) Submit Status 一天,冬马被春希和雪菜拉着去一起去游乐园玩. 经过了各种过山车的洗礼后,三人决定去坐摩天轮休息下. 这是一个巨大的摩天轮,每一个车厢能坐任意多的人.现在,等着坐摩天轮的有n个人(包含他们3人),摩天轮还有m个车厢可以坐人.每个人都有自己肥胖程度,出于某些原因,胖子和瘦子坐在同一节车

UESTC_秋实大哥の恋爱物语 2015 UESTC Training for Search Algorithm &amp; String&lt;Problem K&gt;

K - 秋实大哥の恋爱物语 Time Limit: 5000/2000MS (Java/Others)     Memory Limit: 32000/32000KB (Java/Others) Submit Status 传说有这么一个故事! 在一个月白风清的晚上,秋实大哥约一位他心仪的妹子一起逛校园,浪漫的秋实大哥决定在当晚对妹子表白.“XXXXX...”,秋实大哥温情地说完了准备已久的话.而妹子决定用一种浪漫的方式接受秋实大哥(其实妹子早已对秋实大哥动心,这一刻她早已迫不及待了,但还是决定

【计算几何】【预处理】【枚举】Urozero Autumn Training Camp 2016 Day 5: NWERC-2016 Problem K. Kiwi Trees

发现由于角的度数和边的长度有限制,那俩圆如果放得下的话,必然是塞在两个角里. 于是预处理n个圆心的位置(注意要判断那个圆会不会和其他的边界相交),然后n^2枚举俩角即可. #include<cstdio> #include<cmath> using namespace std; #define EPS 0.00000001 struct Point{ double x,y; double length(){ return sqrt(x*x+y*y); } }a[2010]; typ