ZOJ-1285

全源最短路,floyd一下就完事了,略水

#include<stdio.h>
#include<string.h>
#include<limits.h>

static char wh[30][3];
static int map[30][30];
static int m, n;

static int get_index(char *s)
{
	int i;
	for (i = 0; i < m; i++)
		if (strcmp(wh[i], s) == 0)
			return i;
	return -1;
}

static void floyd()
{
	int i, j, k, a, b;
	for (i = 0; i < m; i++)
		for (j = 0; j < m; j++)
			map[i][j] = i == j ? 0 : INT_MAX;
	char s[3], t[3];
	for (i = 0; i < n; i++)
	{
		scanf("%s %s", s, t);
		a = get_index(s);
		b = get_index(t);
		map[a][b] = map[b][a] = 1;
	}
	for (k = 0; k < m; k++)
		for (i = 0; i < m; i++)
			for (j = 0; j < m; j++)
				if (map[i][k] != INT_MAX && map[k][j] != INT_MAX
						&& map[i][k] + map[k][j] < map[i][j])
					map[i][j] = map[i][k] + map[k][j];
}

int main()
{
	int t, p, it;
	scanf("%d", &t);
	puts("SHIPPING ROUTES OUTPUT\n");
	for (it = 1; it <= t; it++)
	{
		printf("DATA SET %d\n\n", it);
		scanf("%d %d %d", &m, &n, &p);
		getchar();
		int i, cost, a, b;
		for (i = 0; i < m; i++)
			scanf("%s", wh[i]);
		floyd();
		char s[3], t[3];
		for (i = 0; i < p; i++)
		{
			scanf("%d %s %s", &cost, s, t);
			a = get_index(s);
			b = get_index(t);
			if (map[a][b] != INT_MAX)
				printf("$%d\n", map[a][b] * 100 * cost);
			else
				puts("NO SHIPMENT POSSIBLE");
		}
		putchar('\n');
	}
	puts("END OF OUTPUT");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值