hdu 2722 Dijkstra The shortest route

本文介绍了一个基于Dijkstra算法解决特定路径寻找问题的实现案例。通过详细展示代码及输入处理过程,揭示了如何利用该算法在复杂图中找到从起点到终点的最短路径。

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

At the first I think it's a dynamic programming . But the truth hurt my immature soul.It's Dijkstra.At last,I want to say,The input so disgusting.

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

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

#define MAXN 1005

using namespace std;

int cost[MAXN][MAXN];
int mincost[MAXN];
bool vis[MAXN];
int pre[MAXN];
const int INF = 0x3f3f3f3f;
int n,m;

void Dijkstra(int nn,int beg){	
	memset(vis,0,sizeof(vis));
	memset(pre,-1,sizeof(pre));
	memset(mincost,0x3f,sizeof(mincost));
	mincost[beg] = 0;
	for(int j=1;j<=nn;j++){
		int Min = INF,k = -1;
		for(int i=1;i<=nn;i++){
			if(!vis[i]&&mincost[i]<Min){
				Min = mincost[i];
				k = i;
			}
		}
		if(k==-1)break;
		vis[k] = true;
		for(int i=1;i<=nn;i++){
			if(!vis[i]&&mincost[k]+cost[k][i]<mincost[i]){
				mincost[i] = mincost[k] + cost[k][i];
				pre[i] = k;
			}
		}
	}
}

void Solve(){
	Dijkstra((n+1)*(m+1),1);
	if(mincost[(n+1)*(m+1)]==INF){
		puts("Holiday");
	}
	else
		printf("%d blips\n",mincost[(n+1)*(m+1)]);
}

void Input(){
	int tempa,i,j;
	char tempc;
	while(scanf("%d %d",&n,&m),n+m){
        //printf("%d %d\n",n,m);
        memset(cost,0x3f,sizeof(cost));		
        for(i=1;i<=2*n+1;i++){
			if(i&1){
				for(j=1;j<=m;j++){
					scanf("%d %c",&tempa,&tempc);
                    //printf("%d %c ",tempa,tempc);                    
					if(tempa==0){
						continue;
					}
					cost[(i/2)*(m+1)+j][(i/2)*(m+1)+j+1] = 2520/tempa;
					cost[(i/2)*(m+1)+j+1][(i/2)*(m+1)+j] = 2520/tempa;
					if(tempc=='<')cost[(i/2)*(m+1)+j][(i/2)*(m+1)+j+1] = INF;
                    if(tempc=='>')cost[(i/2)*(m+1)+j+1][(i/2)*(m+1)+j] = INF;				
                }
			}
			else {
				for(j=1;j<=m+1;j++){
					scanf("%d %c",&tempa,&tempc);
                    //printf("%d %c ",tempa,tempc);
					if(tempa==0){
						continue;
					}
                    cost[(i/2-1)*(m+1)+j][(i/2)*(m+1)+j] = 2520/tempa;
					cost[(i/2)*(m+1)+j][(i/2-1)*(m+1)+j] = 2520/tempa;
					if(tempc=='^')cost[(i/2-1)*(m+1)+j][(i/2)*(m+1)+j] = INF;						
                    if(tempc=='v')cost[(i/2)*(m+1)+j][(i/2-1)*(m+1)+j] = INF;				
                }			
			}		
		}
		Solve();
	}
}

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值