码迷,mamicode.com
首页 > 其他好文 > 详细

2018HDU多校训练-3-Problem M. Walking Plan

时间:2018-07-31 23:28:04      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:map   bottom   row   health   mes   find   route   mat   tput   

链接:http://acm.hdu.edu.cn/showproblem.php?pid=6331
                                Walking Plan 
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 s技术分享图片i技术分享图片技术分享图片 -th intersection, walk through at least k技术分享图片i技术分享图片技术分享图片 streets and finally return to the t技术分享图片i技术分享图片技术分享图片 -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 u技术分享图片i技术分享图片,v技术分享图片i技术分享图片,w技术分享图片i技术分享图片(1≤u技术分享图片i技术分享图片,v技术分享图片i技术分享图片≤n,u技术分享图片i技术分享图片≠v技术分享图片i技术分享图片,1≤w技术分享图片i技术分享图片≤10000)技术分享图片 , denoting a one way street from the intersection u技术分享图片i技术分享图片技术分享图片 to v技术分享图片i技术分享图片技术分享图片 , and the length of it is w技术分享图片i技术分享图片技术分享图片 .
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 s技术分享图片i技术分享图片,t技术分享图片i技术分享图片,k技术分享图片i技术分享图片(1≤s技术分享图片i技术分享图片,t技术分享图片i技术分享图片≤n,1≤k技术分享图片i技术分享图片≤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
 
Recommend
chendu
 
这题时间复杂度卡的。。。。
题解:这题主要用来分块+DP+Folyd.对于数据范围,我们分100位每一块(一般大一点,我取110  Orz).我们可以先预处理出任意两点间走从0~110步的最短路,然后利用走100为一个单位步,
去更新1*100,2*100,....100*100步的最短路,
由于是至少为K条路的最短路,因此>=k.   我们可以可以再预处理更新一遍恰好走x*100步的情况,查找还有没有于x*100的情况使得i->j的距离变小(因为最多50个点,所以不会超过100)   我们把K 分为K/100,,和K%100,分别求;
参考代码为:
 
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=55,M=110,maxn=10010;
int T,n,m,q,u,v,w,s,t,K;
int a[maxn][N][N],b[maxn][N][N],Map[N][N];
int flag[N][N],dis[N][N];

void pre_work(int x[N][N],int y[N][N],int z[N][N])
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			flag[i][j]=INF;
			for(int k=0;k<n;k++)
				flag[i][j]=min(flag[i][j],x[i][k]+y[k][j]);
		}
	}
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++) z[i][j]=flag[i][j];
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>T;
	while(T--)
	{
		cin>>n>>m;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++) Map[i][j]=INF;
		}
		while(m--)
		{
			cin>>u>>v>>w;
			Map[u-1][v-1]=min(Map[u-1][v-1],w);
		}
		
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++) 
				a[0][i][j]=b[0][i][j]= i==j? 0:INF;
		}
		for(int i=1;i<M;i++) pre_work(a[i-1],Map,a[i]);//处理出经过i步从 x->y 的最短路  
		for(int i=1;i<M;i++) pre_work(b[i-1],a[100],b[i]);//处理出从 x->y 恰好走  100*i步
		
		//Floyd
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++) dis[i][j]= i==j? 0:Map[i][j];
		}
		for(int k=0;k<n;k++)
		{
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<n;j++) dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
			}
		}
		
		for(int x=0;x<M;x++)
		{
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<n;j++)
				{
					flag[i][j]=INF;
					for(int k=0;k<n;k++) flag[i][j]=min(flag[i][j],b[x][i][k]+dis[k][j]); 
				}
			}
			for(int i=0;i<n;i++) for(int j=0;j<n;j++) b[x][i][j]=flag[i][j];	
		}
		
		cin>>q;
		while(q--)
		{
			cin>>s>>t>>K; s--,t--;
			int r=K/100,l=K%100,ans=INF;
			for(int i=0;i<n;i++) ans=min(ans,b[r][s][i]+a[l][i][t]);
			if(ans>=INF) cout<<-1<<endl;
			else cout<<ans<<endl;
		}	
	}
	
	return 0;
}

  

 

2018HDU多校训练-3-Problem M. Walking Plan

标签:map   bottom   row   health   mes   find   route   mat   tput   

原文地址:https://www.cnblogs.com/songorz/p/9398584.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!