本文主要是介绍HDU 2962 Trucking,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Trucking
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1938 Accepted Submission(s): 670
For the given cargo truck, maximizing the height of the goods transported is equivalent to maximizing the amount of goods transported. For safety reasons, there is a certain height limit for the cargo truck which cannot be exceeded.
5 6 1 2 7 5 1 3 4 2 2 4 -1 10 2 5 2 4 3 4 10 1 4 5 8 5 1 5 10 5 6 1 2 7 5 1 3 4 2 2 4 -1 10 2 5 2 4 3 4 10 1 4 5 8 5 1 5 4 3 1 1 2 -1 100 1 3 10 0 0
Case 1: maximum height = 7 length of shortest route = 20Case 2: maximum height = 4 length of shortest route = 8Case 3: cannot reach destination
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#define maxn 1000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;
const int maxm=1005*1005/2;
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int n,m;
int pre[maxn];
struct node
{
int next,v,hight,w;
}edge[maxm];
int vis[maxn];
int dist[maxn];
int h;
void init()
{
int i,x,y,w,hight1;
memset(pre,-1,sizeof(pre));
int Index=1;
for(i=1;i<=m;i++)
{
scanf("%d%d%d%d",&x,&y,&hight1,&w);
if(hight1!=-1)hight1=hight1;
else hight1=INF;
edge[Index].hight=hight1;
edge[Index].v=y;
edge[Index].w=w;
edge[Index].next=pre[x];
pre[x]=Index++;
edge[Index].hight=hight1;
edge[Index].v=x;
edge[Index].w=w;
edge[Index].next=pre[y];
pre[y]=Index++;
}
}
int spfa(int a,int b,int limit)
{
cle(vis);
vis[a]=1;
for(int i=1;i<=n;i++)dist[i]=INF;
dist[a]=0;
queue<int>q;
q.push(a);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int j=pre[u];j!=-1;j=edge[j].next)
{
if(edge[j].hight>=limit)
{
int e=edge[j].v;
if(dist[e]>edge[j].w+dist[u])
{
dist[e]=edge[j].w+dist[u];
if(!vis[e])
{
q.push(e);
vis[e]=1;
}
}
}
}
}
return dist[b];
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int cas=0;int a,b;
while(cin>>n>>m)
{
if(n==0&&m==0)break;
cas++; if(cas!=1)cout<<endl;
init();
scanf("%d%d%d",&a,&b,&h);
int low=1,high=h+1,ans_len=INF,ans_h=0;
while(low<high)
{
int limit=(low+high)>>1;
int r=spfa(a,b,limit);
if(r==INF)
{
high=limit;
}
else
{
low=limit+1;
if(limit>ans_h){ans_h=limit;ans_len=r;}///高度优先
else if(limit==ans_h&&r<ans_len)ans_len=r;///最短路~
}
}
printf("Case %d:\n",cas);
if(ans_len==INF)cout<<"cannot reach destination"<<endl;
else
{
printf("maximum height = %d\n", ans_h);
printf("length of shortest route = %d\n",ans_len);
}
}
return 0;
}
这篇关于HDU 2962 Trucking的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!