hdu 2962 Trucking (最短路径)
Trucking
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1692 Accepted Submission(s): 587
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.
3
1 2 -1 100
一口血喷在了屏幕上...
题目不难而且我的初始思路是对的,开始觉得数据不大没想用二分求高度而已,而且没用二分时间差不大,一倍多。
最后检查了几次,wa了好几次,才发现是没有跳出 0 0!!还以为二分又写错了= =
用C写的邻接表比vector快点。
最短路+枚举高度:
//140MS 600K 1953 B C++
#include<iostream>
#include<queue>
#define N 1005
#define inf 0x3fffffff
using namespace std;
struct node{
int v,h,d;
int next;
}edge[*N];
vector<node>V[N];
int d[N];
int vis[N];
int n,s,e;
int head[N],edgenum;
void addedge(int u,int v,int h,int d)
{
edge[edgenum].v=v;
edge[edgenum].h=h;
edge[edgenum].d=d;
edge[edgenum].next=head[u];
head[u]=edgenum++;
}
void spfa(int s,int maxh)
{
for(int i=;i<=n;i++)
d[i]=inf;
memset(vis,,sizeof(vis));
queue<int>Q;
d[s]=;
Q.push(s);
vis[s]=;
while(!Q.empty()){
int u=Q.front();
Q.pop();
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v;
int h=edge[i].h;
int w=edge[i].d;
if(d[v]>d[u]+w && h>=maxh){
d[v]=d[u]+w;
if(!vis[v]){
Q.push(v);
vis[v]=;
}
}
}
}
}
int main(void)
{
int m,a,b,h,c,k=;
while(scanf("%d%d",&n,&m)!=EOF && (n+m))
{
if(k!=) printf("\n");
memset(head,-,sizeof(head));
for(int i=;i<=n;i++)
V[i].clear();
edgenum=;
for(int i=;i<m;i++){
scanf("%d%d%d%d",&a,&b,&h,&c);
if(h==-) h=inf;
addedge(a,b,h,c);
addedge(b,a,h,c);
}
scanf("%d%d%d",&s,&e,&h);
printf("Case %d:\n",k++);
int ans=inf;
int l=,r=h;
while(l<r){
int mid=(l+r+)>>;
spfa(s,mid);
if(d[e]!=inf){
l=mid;ans=d[e];
}else r=mid-;
}
if(ans!=inf){
printf("maximum height = %d\n",r);
printf("length of shortest route = %d\n",ans);
}else printf("cannot reach destination\n");
}
return ;
}
hdu 2962 Trucking (最短路径)的更多相关文章
- hdu 2962 Trucking (二分+最短路Spfa)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others ...
- HDU - 2962 Trucking SPFA+二分
Trucking A certain local trucking company would like to transport some goods on a cargo truck from o ...
- HDU 2962 Trucking
题目大意:给定无向图,每一条路上都有限重,求能到达目的地的最大限重,同时算出其最短路. 题解:由于有限重,所以二分检索,将二分的值代入最短路中,不断保存和更新即可. #include <cstd ...
- Trucking(HDU 2962 最短路+二分搜索)
Trucking Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Day4 - I - Trucking HDU - 2962
A certain local trucking company would like to transport some goods on a cargo truck from one place ...
- HDU ACM 3790 最短路径问题
最短路径问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- hdu 3790 (最短路径问题dijkstra)
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起 ...
- HDU 2112 HDU Today(最短路径+map)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 1688 Sightseeing (最短路径)
Sightseeing Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- python中将datetime对象转化为时间戳
从mongodb中读取出来的记录中,时间存储在datetime对象里,返回给客户端的却要求是时间戳格式,因此需要将对应的datetime时间转化为时间戳,从stackoverflow上找到同样的问题和 ...
- day 10 形态学处理 膨胀
#-*- coding:utf-8 -*- #1.导入包 import cv2 import numpy as np #2.导入图片 img = cv2.imread('home.jpg',0) #3 ...
- selenium自动化测试资源整理
1. 所有版本chrome下载 是不是很难找到老版本的chrome?博主收集了几个下载chrome老版本的网站,其中哪个下载的是原版的就不得而知了. http://www.slimjet.com/ch ...
- 三个线程ABC,交替打印ABC
转载与:https://www.cnblogs.com/x_wukong/p/4009709.html 创建3个线程,让其交替打印ABC . 输出如下: ABCABCABCABC. 方法:使用syn ...
- leetcode-最长无重复字符的子串
参考他的人代码:https://blog.csdn.net/littlebai07/article/details/79100081 给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: ...
- 学习笔记,99乘法表,嵌套while循环
line = 0 #定义外循环初变量 while line < 9: #外循环判断语句 line += 1 #改变外循环初变量,避免陷入死循环 row = 0 #定义内循环初变量 while r ...
- 初涉 JavaScript
网页是什么 网页 = Html+CSS+JavaScriptHtml:网页元素内容CSS:控制网页样式JavaScript:操作网页内容,实现功能或者效果 JavaScirpt 发展历史 参考 使用 ...
- PAT-甲级解题目录
PAT甲级题目:点这里 pat解题列表 题号 标题 题目类型 10001 1001 A+B Format (20 分) 字符串处理 1003 1003 Emergency (25 分) 最短路径 ...
- 小猫爬山:dfs
题目描述: 翰翰和达达饲养了N只小猫,这天,小猫们要去爬山. 经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了(呜咕>_<). 翰翰和达达只好花钱让它们坐索道下山. ...
- SGU 176 Flow construction(有源汇上下界最小流)
Description 176. Flow construction time limit per test: 1 sec. memory limit per test: 4096 KB input: ...