loj 1379(最短路变形)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27087
思路:题目的意思是求S->T的所有路径中花费总和小于给定的P值的所经过的路径上的最大权值。我们可以从起点做一次SPFA,然后求出起点到所有点的最短路径,然后以终点为起点,将边反向,求终点到起点的最短路,然后枚举每一条边即可,求最大值。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define MAXN 22222
#define MAXM 222222
#define inf 1<<30
#define FILL(a,b) memset(a,b,sizeof(a)) template<class T>inline T Get_Min(const T &a,const T &b){ return a<b?a:b; }
template<class T>inline T Get_Max(const T &a,const T &b){ return a>b?a:b; }
struct Edge{
int v,w,next;
}edge1[MAXM],edge2[MAXM]; int n,m,st,ed,p,NE1,NE2;
int head1[MAXN],head2[MAXN]; void Insert1(int u,int v,int w)
{
edge1[NE1].v=v;
edge1[NE1].w=w;
edge1[NE1].next=head1[u];
head1[u]=NE1++;
} void Insert2(int u,int v,int w)
{
edge2[NE2].v=v;
edge2[NE2].w=w;
edge2[NE2].next=head2[u];
head2[u]=NE2++;
} int dist[][MAXN];
bool mark[MAXN];
void spfa(int st,int ed,Edge *edge,int *dist,int *head)
{
FILL(mark,false);
fill(dist,dist+n+,inf);
queue<int>que;
que.push(st);
dist[st]=;
while(!que.empty()){
int u=que.front();
que.pop();
mark[u]=false;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v,w=edge[i].w;
if(dist[u]+w<dist[v]){
dist[v]=dist[u]+w;
if(!mark[v]){
mark[v]=true;
que.push(v);
}
}
}
}
} int main()
{
int _case,u,v,w,ans,t=;
scanf("%d",&_case);
while(_case--){
scanf("%d%d%d%d%d",&n,&m,&st,&ed,&p);
NE1=NE2=;
FILL(head1,-);
FILL(head2,-);
while(m--){
scanf("%d%d%d",&u,&v,&w);
Insert1(u,v,w);
Insert2(v,u,w);
}
spfa(st,ed,edge1,dist[],head1);
spfa(ed,st,edge2,dist[],head2);
ans=-;
for(int u=;u<=n;u++){
for(int i=head1[u];i!=-;i=edge1[i].next){
int v=edge1[i].v,w=edge1[i].w;
if(dist[][u]!=inf&&dist[][v]!=inf&&dist[][u]+w+dist[][v]<=p){
ans=Get_Max(ans,w);
}
}
}
printf("Case %d: %d\n",t++,ans);
}
return ;
}
loj 1379(最短路变形)的更多相关文章
- POJ-2253.Frogger.(求每条路径中最大值的最小值,最短路变形)
做到了这个题,感觉网上的博客是真的水,只有kuangbin大神一句话就点醒了我,所以我写这篇博客是为了让最短路的入门者尽快脱坑...... 本题思路:本题是最短路的变形,要求出最短路中的最大跳跃距离, ...
- POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...
- POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...
- POJ-1797Heavy Transportation,最短路变形,用dijkstra稍加修改就可以了;
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Description Background Hugo ...
- HDOJ find the safest road 1596【最短路变形】
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HN0I2000最优乘车 (最短路变形)
HN0I2000最优乘车 (最短路变形) 版权声明:本篇随笔版权归作者YJSheep(www.cnblogs.com/yangyaojia)所有,转载请保留原地址! [试题]为了简化城市公共汽车收费系 ...
- 天梯杯 PAT L2-001. 紧急救援 最短路变形
作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上.当其他城市有紧急求 ...
- Heavy Transportation POJ 1797 最短路变形
Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...
- POJ 2253 Frogger ( 最短路变形 || 最小生成树 )
题意 : 给出二维平面上 N 个点,前两个点为起点和终点,问你从起点到终点的所有路径中拥有最短两点间距是多少. 分析 : ① 考虑最小生成树中 Kruskal 算法,在建树的过程中贪心的从最小的边一个 ...
随机推荐
- 【leetcode】Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- C++基础知识面试精选100题系列(21-30)[C++ basics]
[本文链接] http://www.cnblogs.com/hellogiser/p/100-interview-questions-of-cplusplus-basics-21-30.html [题 ...
- iOS 关于UIWindow的理解
Every iOS app has a window that handles the presentation of the app’s user interface. Although the w ...
- FastReport里面正确调用函数的方法
FastReport里面正确调用函数的方法 错误: [FormatDateTime('yyyy-mm-dd',[frxDBDataset1."日期"])] --------- ...
- Java for LintCode 链表插入排序
用插入排序对链表排序 解题思路: 最省时间的方法是使用优先级队列,但是无法通过,那就直接插入排序好了. public ListNode insertionSortList(ListNode head) ...
- SQL Server 2008登录错误:无法连接到(local)的解决方法
1.服务器类型我们选择了“数据库引擎”时,查找里面的可登录用户名是没有的,下边的服务器名称只显示为“(local)”,连“Windows 身份验证”都无法登录. 如果朋友们和我出错的问题是一样请看下面 ...
- codeforces 490C. Hacking Cypher 解题报告
题目链接:http://codeforces.com/problemset/problem/490/C 题目意思:给出一个可能有10^6 位长的字符串且没有前导0的整数,问能否一分为二,使得前面的一部 ...
- IOS-CoreAnimation
核心动画 •Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍! •使用它需要先添加QuartzCore.framework和引入对应的框架 ...
- Spring面向切面编程(AOP)方式二
使用注解进行实现:减少xml文件的配置. 1 建立切面类 不需要实现任何特定接口,按照需要自己定义通知. package org.guangsoft.utils; import java.util.D ...
- ios手势
iOS 手势操作:拖动.捏合.旋转.点按.长按.轻扫.自定义 大 中 小 1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. i ...