hdu 1224 Free DIY Tour Flyod

本文介绍了一个使用Floyd算法解决的问题实例,通过详细展示输入、计算路径成本和能量消耗的过程,来说明Floyd算法的强大功能及其在寻找最短路径问题中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Another Problem be solved by Flyod.Flyod is so powerful.

The portal:http://acm.hdu.edu.cn/showproblem.php?pid=1224

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

#define MAXN 105
const int INF = 0x3f3f3f3f;

int cost[MAXN][MAXN];
int route[MAXN][MAXN];
int power[MAXN];

void Input(){
	int T,n,t=0;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);		
		for(int i=1;i<=n;i++){
			scanf("%d",power+i);			
		}
		for(int i=1;i<=n+1;i++){
			for(int j=1;j<=n+1;j++){
				cost[i][j] = INF;			
			}	
		}
		for(int i=1;i<=n+1;i++){
			for(int j=1;j<=n+1;j++){
				route[i][j] = j;			
			}		
		}
		int m;
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			int tempa,tempb;
			scanf("%d %d",&tempa,&tempb);
			cost[tempa][tempb] = 0;		
		}
		for(int k=1;k<=n+1;k++){
			for(int i=1;i<=n+1;i++){
				for(int j=1;j<=n+1;j++){
					if(cost[i][k]==INF||cost[k][j]==INF)continue;
					int temp = cost[i][k] + cost[k][j] +power[k] ;					
					if(cost[i][j]==INF||temp > cost[i][j]){
						cost[i][j] = temp;
						route[i][j] = route[i][k];					
					}				
				}
			}				
		}
		if(t++)puts("");
		printf("CASE %d#\n",t);
		printf("points : %d\n",cost[1][n+1]);
		printf("circuit : ");
		int t = 1;
		printf("1");
		t = route[t][n+1];		
		while(t!=n+1){
			printf("->%d",t);
			t = route[t][n+1];
		}
		printf("->%d",1);
		puts("");
	}
}

int main(){
	//freopen("1224.in","r",stdin);
	Input();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值