Problem K: Yikes -- Bikes!

时间:2023-03-09 20:38:41
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[][];
int c[];
int topo[];
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[];
while(cin >> n && n){
for(int i = ;i <= n;i++){
cin >> name[i];
un[ name[i] ] = i;
}
memset(graph,,sizeof(graph));
for(int i = ;i <= n-;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] = - *num;
//cout <<a << " " << b << endl;
}
for(int k = ;k <= n;k++){
for(int i = ;i <= n;i++){
for(int j = ;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] > && graph[k][j] > ){
graph[i][j] = graph[i][k] * graph[k][j];
graph[j][i] = - * graph[i][k] * graph[k][j];
}
else if(graph[i][k] < && graph[k][j] < ){
graph[j][i] = graph[i][k] * graph[k][j];
graph[i][j] = - * graph[i][k] * graph[k][j];
}
else if(graph[i][k] < && graph[k][j] > ){
if(abs(graph[i][k]) > graph[k][j]){
graph[j][i] = abs(graph[i][k]) / graph[k][j];
graph[i][j] = - * graph[j][i];
}
else{
graph[i][j] = abs(graph[i][k]) / graph[k][j];
graph[j][i] = - * 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] = - * graph[i][j];
}
else{
graph[j][i] = graph[i][k] / abs(graph[k][j]);
graph[i][j] = - * graph[j][i];
}
}
}
}
}
}
int mark;
for(int i = ;i <= n;i++){
bool flag = ;;
for(int j = ;j <= n;j++){
if(graph[i][j] < ){
flag = ;
break;
}
}
if(flag){
mark = i;
break;
}
}
Value a[];
for(int i = ;i <= n;i++){
a[i-].x = graph[mark][i];
a[i-].y = i;
}
sort(a,a+n,cmp);
cout << << name[ a[n-].y ];
for(int i = n-;i > -;i--){
cout << " = "<< a[i].x << name[ a[i].y ] ;
}
cout << endl;
}
return ;
}