HDU1385—— Minimum Transport Cost

HDU1385—— Minimum Transport Cost

题目描述

An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending with b.
Write a program that: finds the minimal number of elements in a set containing at least two different integers from each interval.
Input
The first line of the input contains the number of intervals n, 1 <= n <= 10000. Each of the following n lines contains two integers a, b separated by a single space, 0 <= a < b <= 10000. They are the beginning and the end of an interval.
Output
Output the minimal number of elements in a set containing at least two different integers from each interval.
Sample Input

4
3 6
2 4
0 2
4 7

Sample Output

4

记录要点

1.floyed如果记录路径?
ans:

		for(k = 1 ; k <= n ; k ++ ){
			for(i = 1 ; i <= n ; i ++ ){
				for(j = 1 ; j <= n ; j ++ ){
					if(dp[i][j] > (dp[i][k]+dp[k][j] + cost[k])){
						dp[i][j] = dp[i][k]+dp[k][j] + cost[k];
						path[i][j] = path[i][k];//记录i , j 之间,最左边的那个点	
					}
				}
			}
		}

输出

			while(u!=path[u][v]){
				cout<<u<<"-->";u = path[u][v];
			}
			cout<<u<<endl;

2.关于评测机空间memory的一些猜测:
如果使用了memset函数,就会直接开辟整个函数的空间,不然就只开辟使用了的空间
比如这个题,如果使用memset函数初始化dp和path就会memoey limit exceed。
在这里插入图片描述从上到下:

  1. 没有使用memset+2500*2500
  2. 使用了memset + 2500*2500
  3. 没有使用memset + 2500*2500
  4. 使用memset + 550*550
  5. 使用了memset + 2500*2500

做题地址

链接: hdu1385.

AC代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<memory.h>
#include<cstdio>
using namespace std;
typedef long long int LL; 
const int inf = 0x3f3f3f3f;
const int N = 2550;

int n , dp[N][N] , cost[N] , path[N][N];
int main(){
	std::ios::sync_with_stdio(false);
	int i , j , tem , u , v , k;
	while(cin>>n&&n){
		for(i = 1 ; i <= n ; i ++ ){
			for(j = 1 ; j <= n ; j ++ ){
				cin>>tem;
				if(tem == -1) dp[i][j] = inf;
				dp[i][j] = tem;
				path[i][j] = j;
			}
		}
		for(i = 1 ; i <= n ; i ++ ) cin>>cost[i];
		for(k = 1 ; k <= n ; k ++ ){
			for(i = 1 ; i <= n ; i ++ ){
				for(j = 1 ; j <= n ; j ++ ){
					if(dp[i][j] > (dp[i][k]+dp[k][j] + cost[k])){
						dp[i][j] = dp[i][k]+dp[k][j] + cost[k];
						path[i][j] = path[i][k];	
					}else if(dp[i][j] == (dp[i][k]+dp[k][j] + cost[k])){
						if(path[i][j] > path[i][k]){
							path[i][j] = path[i][k];
						}
					}
				}
			}
		}
		while(cin>>u>>v&&(u!=-1&&v!=-1)){
			int from = u , to = v;
			cout<<"From "<<u<<" to "<<v<<" :"<<endl<<"Path: ";
			while(u!=path[u][v]){
				cout<<u<<"-->";u = path[u][v];
			}
			cout<<u<<endl;
			cout<<"Total cost : "<<dp[from][to]<<endl;
			cout<<endl;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值