spoj-SAMER08A-最短路
SAMER08A - Almost Shortest Path
Finding the shortest path that goes from a starting point to a destination point given a set of points and route lengths connecting them is an already well known problem, and it's even part of our daily lives, as shortest path programs are widely available nowadays.
Most people usually like very much these applications as they make their lives easier. Well, maybe not that much easier.
Now that almost everyone can have access to GPS navigation devices able
to calculate shortest paths, most routes that form the shortest path
are getting slower because of heavy traffic. As most people try to
follow the same path, it's not worth it anymore to follow these
directions.
With this in his mind, your boss asks you to develop
a new application that only he will have access to, thus saving him
time whenever he has a meeting or any urgent event. He asks you that the
program must answer not the shortest path, but the almost shortest
path. He defines the almost shortest path as the shortest path that goes
from a starting point to a destination point such that no route between
two consecutive points belongs to any shortest path from the starting
point to the destination.
For example, suppose the figure below
represents the map given, with circles representing location points, and
lines representing direct, one-way routes with lengths indicated. The
starting point is marked as S and the destination point is marked as D.
The bold lines belong to a shortest path (in this case there are two
shortest paths, each with total length 4). Thus, the almost shortest
path would be the one indicated by dashed lines (total length 5), as no
route between two consecutive points belongs to any shortest path.
Notice that there could exist more than one possible answer, for
instance if the route with length 3 had length 1. There could exist no
possible answer as well.
Input
The input contains several test cases. The first line of a test case contains two integers N (2 ≤ N ≤ 500) and M (1 ≤ M ≤ 104),
separated by a single space, indicating respectively the number of
points in the map and the number of existing one-way routes connecting
two points directly. Each point is identified by an integer between 0
and N -1. The second line contains two integers S and D, separated by a single space, indicating respectively the starting and the destination points (S ≠ D; 0 ≤ S, D < N).
Each one of the following M lines contains three integers U, V and P (U ≠ V; 0 ≤ U, V < N; 1 ≤ P ≤ 103), separated by single spaces, indicating the existence of a one-way route from U to V with distance P. There is at most one route from a given point U to a given point V, but notice that the existence of a route from U to V does not imply there is a route from V to U,
and, if such road exists, it can have a different length. The end of
input is indicated by a line containing only two zeros separated by a
single space.
Output
For each test case in the input, your program must print a single line, containing -1 if it is not possible to match the requirements, or an integer representing the length of the almost shortest path found.
Example
Input:
7 9
0 6
0 1 1
0 2 1
0 3 2
0 4 3
1 5 2
2 6 4
3 6 2
4 6 4
5 6 1
4 6
0 2
0 1 1
1 2 1
1 3 1
3 2 1
2 0 3
3 0 2
6 8
0 1
0 1 1
0 2 2
0 3 3
2 5 3
3 4 2
4 1 1
5 1 1
3 0 1
0 0 Output:
5
-1
6 题意是给出一个单向图,然后只要这条路径(S->D)的长度和最短路的长度一致,那么这条路上所有的边都删掉之后,再跑一次从S->D的最短路。
反向建边后跑两次dij,然后枚举所有边将属于最短路的边删掉。
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define mp make_pair
struct Edge{
int u,v,w,next;
bool o;
}e1[],e2[];
int tot1,tot2,first1[],first2[];
void add1(int u,int v,int w){
e1[tot1].u=u;
e1[tot1].v=v;
e1[tot1].o=;
e1[tot1].w=w;
e1[tot1].next=first1[u];
first1[u]=tot1++;
}
void add2(int u,int v,int w){
e2[tot2].u=u;
e2[tot2].v=v;
e2[tot2].o=;
e2[tot2].w=w;
e2[tot2].next=first2[u];
first2[u]=tot2++;
}
int N,M,S,D,i,j,k;
bool vis[];
int d1[],d2[];
int dij(int S,int D,int d[],Edge e[],int first[]){
memset(d,inf,sizeof(int)*);
memset(vis,,sizeof(bool)*);
priority_queue<pii,vector<pii>,greater<pii> > q;
q.push(mp(,S));
d[S]=;
while(!q.empty()){
int u=q.top().second;
q.pop();
if(vis[u]) continue;
vis[u]=;
for(int i=first[u];i+;i=e[i].next){
if(e[i].o&&d[e[i].v]>d[u]+e[i].w){
d[e[i].v]=d[u]+e[i].w;
q.push(mp(d[e[i].v],e[i].v));
}
}
}
return d[D]==inf?-:d[D];
}
int main()
{
while(cin>>N>>M&&(N||M)){int u,v,w;
cin>>S>>D;
memset(first1,-,sizeof(first1));
memset(first2,-,sizeof(first2));
tot1=tot2=;
while(M--){
scanf("%d%d%d",&u,&v,&w);
add1(u,v,w);
add2(v,u,w);
}
int minn=dij(S,D,d1,e1,first1);
dij(D,S,d2,e2,first2);
for(i=;i<tot1;++i){
if(e1[i].w+d1[e1[i].u]+d2[e1[i].v]==minn) e1[i].o=;
}
cout<<dij(S,D,d1,e1,first1)<<endl;
}
return ;
}
spoj-SAMER08A-最短路的更多相关文章
- POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)
POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...
- SPOJ OTOCI 动态树 LCT
SPOJ OTOCI 裸的动态树问题. 回顾一下我们对树的认识. 最初,它是一个连通的无向的无环的图,然后我们发现由一个根出发进行BFS 会出现层次分明的树状图形. 然后根据树的递归和层次性质,我们得 ...
- bzoj1001--最大流转最短路
http://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路:这应该算是经典的最大流求最小割吧.不过题目中n,m<=1000,用最大流会TLE, ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- 【USACO 3.2】Sweet Butter(最短路)
题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...
- Sicily 1031: Campus (最短路)
这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...
- 最短路(Floyd)
关于最短的先记下了 Floyd算法: 1.比较精简准确的关于Floyd思想的表达:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B.所以,我们假设maz ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- bzoj1266最短路+最小割
本来写了spfa wa了 看到网上有人写Floyd过了 表示不开心 ̄へ ̄ 改成Floyd试试... 还是wa ヾ(。`Д´。)原来是建图错了(样例怎么过的) 结果T了 于是把Floyd改回spfa 还 ...
随机推荐
- 等待事件对应的p1,p2,p3含义
Oracle 10g v$session视图中不同等待事件对应的p1,p2,p3的含义也不同,我们不可能记住所有等待事件对应的p1,p2,p3的含义. 可以通过查询V$EVENT_NAME知道每个等待 ...
- 最值得阅读学习的 10 个 C 语言开源项目代码
1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能,最多可以模拟3万个并发连 ...
- split_lzo_lib.sh
split_lzo_lib.sh #!/bin/sh#输入文件名filename=$1#分割文件大小filesize=4096#输出库文件名libname="lib"$(echo ...
- manager
S 识别 M 买账 A-安排 R-认同 T-提问识别上级的沟通特点,判断形势,识别沟通的时机摆正自己的角色位置,礼多人不怪,回应情绪做好沟通准备,有策略,安排合适时间听取反馈意见,认同并接纳指导提问 ...
- 多媒体文件格式分析 MP3文件结构及编解码流程
多媒体文件格式分析 http://blog.csdn.net/taniya001/article/details/7962864 多媒体文件格式分析 MP3文件结构及编解码流程 http://www. ...
- c调用c++编的dll,c++调用c编写的dll,extern “C”的用法
转自:http://blog.csdn.net/life_is_too_hard/article/details/52137271 c和c++不能直接相互调用,主要是因为c++有重载函数的功能,为了区 ...
- [one day one question] Iscroll 5.0 在chrome上无法滑动
问题描述: Iscroll 5.0 在chrome上无法滑动,不仅仅在chromePC的开发的时候,在手机上的chrome也有同样的问题,这怎么破? 解决方案: // 关闭 PointerEvent ...
- iframe自适应高度(兼容多种浏览器)
http://jingyan.baidu.com/article/b87fe19eaeb2cf5218356896.html 让iframe自适应高度,下面是实现的源码: <div id=&qu ...
- 20145230熊佳炜《逆向及BOF基础实践》
20145230熊佳炜<逆向及BOF基础实践> 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件.该程序同时包含另一个代码片段,getShell,会返回一个可用Shell. ...
- 20145211 《网络渗透》MS08_067安全漏洞
20145211 <网络渗透>MS08_067安全漏洞 一.实验原理 ms08_067是服务器服务中一个秘密报告的漏洞,于2008年被发现.攻击者利用靶机默认开放的SMB服务的445端口, ...