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。
从上到下:
- 没有使用memset+2500*2500
- 使用了memset + 2500*2500
- 没有使用memset + 2500*2500
- 使用memset + 550*550
- 使用了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;
}