hdu 3631(floyd)

7 篇文章 0 订阅

题目:Shortest Path

题意:一张图有n个顶点m条边,现在给q个查询。查询0 x,标记顶点x。查询1 x y,查找x和y之间通过标记点的最短路。

题解:每次查询做一次floyd,对floyd还是理解的不够透彻。不是很明白原理。

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <string>
using namespace std;
#define For(i,a) for(i=0;i<a;i++)
#define Foru(i,a,b) for(i=a;i<=b;i++)
#define Ford(i,a,b) for(i=a;i>=b;i--)
#define clr(ar,vel) memset(ar,vel,sizeof(ar))
#define PB push_back
typedef long long ll;
const int maxint = 0x7fffffff;
const ll maxll = 1LL<<60;
const int maxn = 310;
const int inf = 99999999;
int n, m, q;
int ans[maxn][maxn];
int mark[maxn];
int floyd(int x){
	for(int i = 0; i < n; i ++){
		for(int j = 0; j < n; j ++){
			ans[i][j] = min( ans[i][j], ans[i][x] + ans[x][j]);
		}
	}
}
int main(){
	int x, y, vel, t= 1;
	while(1){
		scanf("%d%d%d",&n,&m,&q);
		if( (n|m|q) == 0) break;
		
		for(int i = 0; i < maxn; i ++){
			for(int j = 0; j < maxn; j ++){
				ans[i][j] = inf;
				ans[i][i] = 0;
			}
		}
		
		memset(mark, 0, sizeof(mark));
		while(m --){
			scanf("%d%d%d",&x,&y,&vel);
			ans[x][y] = min(ans[x][y], vel);
		}
		if( t>1) printf("\n");
		printf("Case %d:\n",t++); 
		while(q --){
			scanf("%d",&vel);
			if( vel == 1 ){ 
				scanf("%d%d",&x,&y);
				if( mark[x] && mark[y] ) {
					if( ans[x][y] >= 99999999 ) printf("No such path\n");  
					else printf("%d\n",ans[x][y]);
				}
				else printf("ERROR! At path %d to %d\n",x,y);
			}
			else {
				scanf("%d",&x);
				if( mark[x] ) printf("ERROR! At point %d\n",x);
				else {
					mark[x] = 1;
					floyd(x);
				}
			}
		}
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值