hdu6331(最短路+DP)

11 篇文章 0 订阅

这个主要是考flyod的本质应用。。貌似没理解到位这次没做出来qaq,其实和上次宁夏挺像的。。

设d[k][i][j]为从i到j走k步的最短路然后和原边依次合并就是转移了。。

直接转移肯定是不行的。。可以想一下优化。。

这个转移方程其实和矩阵乘法大同小异(加法变成了min),所以一个思路是快速幂求(貌似dls最初的解法)。。

然而询问是O(qnlogn)这个比较极限。。估计会被卡。。(这场疯狂被卡qaq)

题解是用分块做,预处理是O(n^3*sqrt(k)),有点极限不过没上面那么极限就对了。。(被cls安排得明明白白)

具体就是分成整百和余百来做。。提前预处理好就行。。复杂度O(qn+n^3*sqrt(k))。。

分块做的时候需要注意边数和最短路并不严格单调,即多走几步可能会得到更优的解。。所以在对余数预处理的时候得多跑几条边,走多n条边可以说是很稳的了。。

 

 

 

/**
 *          ┏┓    ┏┓
 *          ┏┛┗━━━━━━━┛┗━━━┓
 *          ┃       ┃  
 *          ┃   ━    ┃
 *          ┃ >   < ┃
 *          ┃       ┃
 *          ┃... ⌒ ...  ┃
 *          ┃              ┃
 *          ┗━┓          ┏━┛
 *          ┃          ┃ Code is far away from bug with the animal protecting          
 *          ┃          ┃   神兽保佑,代码无bug
 *          ┃          ┃           
 *          ┃          ┃        
 *          ┃          ┃
 *          ┃          ┃           
 *          ┃          ┗━━━┓
 *          ┃              ┣┓
 *          ┃              ┏┛
 *          ┗┓┓┏━━━━━━━━┳┓┏┛
 *           ┃┫┫       ┃┫┫
 *           ┗┻┛       ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 55
#define nm 105
#define N 1000005
#define M(x,y) x=max(x,y)
const double pi=acos(-1);
const ll inf=1e9+7;
using namespace std;
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}
 
 




int n,m,f[nm][NM][NM],d[150][NM][NM],_x,_y,_t;

int main(){
    int _=read();while(_--){
	n=read();m=read();
	inc(i,1,n)inc(j,1,n)d[1][i][j]=inf;
	inc(i,1,m){_x=read();_y=read();d[1][_x][_y]=min(d[1][_x][_y],read());}
	inc(i,1,n)inc(j,1,n)d[0][i][j]=d[1][i][j];
	inc(i,1,n)d[0][i][i]=0;
	inc(p,2,150){
	    inc(i,1,n)inc(j,1,n)d[p][i][j]=inf;
	    inc(k,1,n)inc(i,1,n)inc(j,1,n)d[p][i][j]=min(d[p][i][j],d[p-1][i][k]+d[1][k][j]);
	}
	inc(i,1,n)inc(j,1,n)f[1][i][j]=d[100][i][j];
	inc(p,2,100){
	    inc(i,1,n)inc(j,1,n)f[p][i][j]=inf;
	    inc(k,1,n)inc(i,1,n)inc(j,1,n)f[p][i][j]=min(f[p][i][j],f[p-1][i][k]+f[1][k][j]);
	}
	dec(p,149,0)inc(i,1,n)inc(j,1,n)d[p][i][j]=min(d[p][i][j],d[p+1][i][j]);
	m=read();while(m--){
	    _x=read();_y=read();_t=read();
	    int s=inf;
	    if(_t>100)inc(i,1,n)s=min(s,f[_t/100][_x][i]+d[_t%100][i][_y]);
	    if(_t<=100)s=min(s,d[_t][_x][_y]);
	    if(_t%100==0)s=min(s,f[_t/100][_x][_y]);
	    if(s<inf)printf("%d\n",s);else printf("-1\n");
	}
    }
    return 0;
}

 

 

Problem M. Walking Plan

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 409    Accepted Submission(s): 136


 

Problem Description

There are n intersections in Bytetown, connected with m one way streets. Little Q likes sport walking very much, he plans to walk for q days. On the i-th day, Little Q plans to start walking at the si-th intersection, walk through at least ki streets and finally return to the ti-th intersection.
Little Q's smart phone will record his walking route. Compared to stay healthy, Little Q cares the statistics more. So he wants to minimize the total walking length of each day. Please write a program to help him find the best route.

 

 

Input

The first line of the input contains an integer T(1≤T≤10), denoting the number of test cases.
In each test case, there are 2 integers n,m(2≤n≤50,1≤m≤10000) in the first line, denoting the number of intersections and one way streets.
In the next m lines, each line contains 3 integers ui,vi,wi(1≤ui,vin,uivi,1≤wi≤10000), denoting a one way street from the intersection ui to vi, and the length of it is wi.
Then in the next line, there is an integer q(1≤q≤100000), denoting the number of days.
In the next q lines, each line contains 3 integers si,ti,ki(1≤si,tin,1≤ki≤10000), describing the walking plan.

 

 

Output

For each walking plan, print a single line containing an integer, denoting the minimum total walking length. If there is no solution, please print -1.

 

 

Sample Input

 

2 3 3 1 2 1 2 3 10 3 1 100 3 1 1 1 1 2 1 1 3 1 2 1 1 2 1 1 2 1 1

 

 

Sample Output

 

111 1 11 -1

 

 

Source

2018 Multi-University Training Contest 3

 

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6331 6330 6329 6328 6327 

 

 

Statistic | Submit | Discuss | Note

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值