ZOJ Problem Set - 1092 Arbitrage (Floyd)
来源:互联网 发布:探险家软件 编辑:程序博客网 时间:2024/04/27 17:55
Floyd-Warshall算法描述:
For k←1 to n do // k为“媒介节点” For i←1 to n do For j←1 to n do if (dist(i,k) + dist(k,j) < dist(i,j)) then // 是否是更短的路径? dist(i,j) = dist(i,k) + dist(k,j)
由于本题是求最大的收益,因此描述如下:
for (k = 1; k <= n; k++)for (i = 1; i <= n; i++)for (j = 1; j <= n; j++)if (map[i][j] < map[i][k] * map[k][j])map[i][j] = map[i][k] * map[k][j];
解题如下:
#include <stdio.h>#include <stdlib.h>#include <string.h>double map[32][32];int n;void input(int N){int j, i, x, y, m, flag;double price;char name[32][100];char temp1[100], temp2[100];for (i = 1; i <= N; i++)scanf("%s", name[i]);scanf("%d", &m);for (i = 1; i <= N; i++){for (j = 1; j <= N; j++)map[i][j] = 0.0;map[i][i] = 1.0;}while (m--){scanf("%s %lf %s", temp1, &price, temp2);flag = 0;for (i = 1; i <= N && flag != 2; i++){if (strcmp(temp1, name[i]) == 0){x = i;flag++;}if (strcmp(temp2, name[i]) == 0){y = i;flag++;}}map[x][y] = price;}}void floyd(int *success){int k, i, j;for (k = 1; k <= n; k++)for (i = 1; i <= n; i++)for (j = 1; j <= n; j++)if (map[i][j] < map[i][k] * map[k][j])map[i][j] = map[i][k] * map[k][j];for (k = 1; k <= n; k++)if (map[k][k] > 1){*success = 1;break;}}int main(){int k, i, j;int success, count = 1;while (scanf("%d", &n) && n){success = 0;input(n);floyd(&success);if (!success)printf("Case %d: No\n", count++);elseprintf("Case %d: Yes\n", count++);}return 0;}
0 0
- ZOJ Problem Set - 1092 Arbitrage (Floyd)
- ZOJ Problem Set - 1092Arbitrage
- ZOJ 1092 Arbitrage (floyd变形)
- ZOJ 1092 Arbitrage 【Floyd-Warshal】
- ZOJ 1092 Arbitrage Floyd算法
- zoj 1092 && poj 2240 Arbitrage (floyd算法)
- zoj 1092 || poj 2240 Arbitrage(Floyd)
- zoj 1092 Arbitrage(最短路,floyd)
- zoj 1092 Arbitrage
- zoj 1092 Arbitrage
- zoj-1092-Arbitrage
- zoj 1092 Arbitrage
- ZOJ Problem Set - 2060
- ZOJ Problem Set - 2972
- ZOJ Problem Set - 1037
- ZOJ Problem Set - 1048
- ZOJ Problem Set - 1049
- ZOJ Problem Set - 1051
- [leetcode][dfs] Lowest Common Ancestor of a Binary Search Tree
- Java多线程(二)、线程的生命周期和状态控制
- Servlet3.0入门学习(一)
- 关于mac/linux下vim的语法高亮与行号显示!
- 55分钟学会正则表达式(纠正了翻译中的一些错误)
- ZOJ Problem Set - 1092 Arbitrage (Floyd)
- cf Alter Board
- Android-动画
- CSS3文字立体效果
- android 实践 - 2015-7-12
- 关于mac开机时的一些命令
- hdu 1285 确定比赛名次
- Hbase namespace问题
- The Skyline Problem