POJ 3463 Sightseeing (最短路 次短路)

Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one city S to another city F. On this way, the tourists in the bus can see the sights alongside the route travelled. Moreover, the bus makes a number of stops (zero or more) at some beautiful cities, where the tourists get out to see the local sights.

Different groups of tourists may have different preferences for the sights they want to see, and thus for the route to be taken from S to F. Therefore, Your Personal Holiday wants to offer its clients a choice from many different routes. As hotels have been booked in advance, the starting city S and the final city F, though, are fixed. Two routes from S to F are considered different if there is at least one road from a city A to a city B which is part of one route, but not of the other route.

There is a restriction on the routes that the tourists may choose from. To leave enough time for the sightseeing at the stops (and to avoid using too much fuel), the bus has to take a short route from S to F. It has to be either a route with minimal distance, or a route which is one distance unit longer than the minimal distance. Indeed, by allowing routes that are one distance unit longer, the tourists may have more choice than by restricting them to exactly the minimal routes. This enhances the impression of a personal holiday.

For example, for the above road map, there are two minimal routes from S = 1 to F = 5: 1 → 2 → 5 and 1 → 3 → 5, both of length 6. There is one route that is one distance unit longer: 1 → 3 → 4 → 5, of length 7.

Now, given a (partial) road map of the Benelux and two cities S and F, tour operator Your Personal Holiday likes to know how many different routes it can offer to its clients, under the above restriction on the route length.

Input
The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

One line with two integers N and M, separated by a single space, with 2 ≤ N ≤ 1,000 and 1 ≤ M ≤ 10, 000: the number of cities and the number of roads in the road map.

M lines, each with three integers A, B and L, separated by single spaces, with 1 ≤ A, B ≤ N, A ≠ B and 1 ≤ L ≤ 1,000, describing a road from city A to city B with length L.

The roads are unidirectional. Hence, if there is a road from A to B, then there is not necessarily also a road from B to A. There may be different roads from a city A to a city B.

One line with two integers S and F, separated by a single space, with 1 ≤ S, F ≤ N and S ≠ F: the starting city and the final city of the route.

There will be at least one route from S to F.

Output
For every test case in the input file, the output should contain a single number, on a single line: the number of routes of minimal length or one distance unit longer. Test cases are such, that this number is at most 109 = 1,000,000,000.

Sample Input
2
5 8
1 2 3
1 3 2
1 4 5
2 3 1
2 5 3
3 4 2
3 5 4
4 5 3
1 5
5 6
2 3 1
3 2 1
3 1 10
4 5 2
5 2 7
5 2 7
4 1
Sample Output
3
2
Hint
The first test case above corresponds to the picture in the problem description.

题意

询问最短路径和次短路径的条数,(次短路径的长度必须是最短路径的单位长度+1)。

题解

1.if(x<最小)更新最短路和次短路
2.if(x==最小)更新最短路数量
3.if(x<次小)更新次短路
4.if(x==次小)更新次短路数量

记录数量用cnt数组记录一下, 需要更新某个结点最短路的时候把前驱结点的数组赋值给此结点,如果恰好距离相等,就是加法原理了。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int maxn=1005;
#define ms(a,n) memset(a,n,sizeof(a))
inline LL read()
{
    LL x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
struct edge{
    int s,cost,pos;
    bool operator <(const edge& x)const
    {
        return cost>x.cost;//边权小的优先
    }
};
typedef pair<int,int> P;//first为顶点编号,second为权值
int n,m;
int dist[maxn][2];
int cnt[maxn][2];
bool vis[maxn][2];
vector<P> G[maxn];
void dijkstra(int s,int f)
{
    priority_queue<edge> que;
    ms(dist,INF);
    ms(cnt,0);
    ms(vis,false);
    cnt[s][0]=1;
    dist[s][0]=0;
    que.push((edge){s,0,0});
    while(!que.empty())
    {
        edge p=que.top();que.pop();
        if(vis[p.s][p.pos])
            continue;
        int u=p.s,t=p.pos;
        vis[u][t]=true;
        for(int i=0;i<G[u].size();i++)
        {
            int v=G[u][i].first,d=G[u][i].second;
            if(dist[v][0]>d+dist[u][t])
            {
                dist[v][1]=dist[v][0];
                cnt[v][1]=cnt[v][0];
                dist[v][0]=d+dist[u][t];
                cnt[v][0]=cnt[u][t];
                que.push((edge){v,dist[v][0],0});
                que.push((edge){v,dist[v][1],1});
            }
            else if(dist[v][0]==d+dist[u][t])
                cnt[v][0]+=cnt[u][t];
            else if(dist[v][1]>d+dist[u][t])
            {
                dist[v][1]=d+dist[u][t];
                cnt[v][1]=cnt[u][t];
                que.push((edge){v,dist[v][1],1});
            }
            else if(dist[v][1]==d+dist[u][t])
                cnt[v][1]+=cnt[u][t];
        }
    }
    int ans=cnt[f][0];
    if(dist[f][1]==dist[f][0]+1)
        ans+=cnt[f][1];
    printf("%d\n",ans);
}
void clear()
{
    for(int i=1;i<=n;i++)
        G[i].clear();
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        clear();
        for(int i=0;i<m;i++)
        {
            int u,v,c;
            scanf("%d%d%d",&u,&v,&c);
            G[u].push_back(P(v,c));
        }
        int s,f;
        scanf("%d%d",&s,&f);
        dijkstra(s,f);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/orion7/p/8570221.html

时间: 2024-08-04 02:55:34

POJ 3463 Sightseeing (最短路 次短路)的相关文章

poj 3463 Sightseeing (dij 求最短路和次短路并计数)

dijkstra求最短路和次短路的求法和计算  模板 dijkstra求最短路的变形. 外循环要循环2*n-1次,因为dis[n][2]有2*n个状态,而dis[s][0]已经用过一次. 算法: 1.如果比最短路短就更新最短路和次短路. 2.如果和最短路相等,更新最短路的计数. 3.如果和次短路相等,更新次短路的方法数. 4.如果比次短路短,更新次短路. #include<cstdio> #include<iostream> #include<cstring> #inc

poj 3463 Sightseeing(最短路+次短路)

http://poj.org/problem?id=3463 大致题意:给出一个有向图,从起点到终点求出最短路和次短路的条数之和. 解法: 用到的数组:dis[i][0]:i到起点的最短路,dis[i][1]:i到起点的严格次短路 vis[i][0],vis[i][1]:同一维的vis数组,标记距离是否已确定 sum[i][0]:i到起点的最短路条数,sum[i][1]:i到起点的次短路条数 同一维dijkstra,内循环先找出最短的距离(次短路或最短路)d,然后枚举与该点相连的点: if(d

poj 3463 Sightseeing 最短路径数量

题意: 求有向图中最短路和比最短路大1的路径数量. 思路: 需要理解dijkstra算法中dis[n]数组的含义,设cnt[i]表示到点i的最短路径数量,cnt1[i]表示到点i比最短路大1的路径数量.在运行dijkstra算法的过程中每次获得最小dis[i]的时候可以对所有dis[v]+w(v,i)==dis[i]的v做如下更新cnt[i]+=cnt[v],cnt1[i]+=cnt1[v].而当所有值为某数的dis[i]计算完成时也就是对任意i,dis[i]为同一值且不再变化时,可以对这些满足

poj 3463 Sightseeing——次短路计数

题目:http://poj.org/problem?id=3463 当然要给一个点记最短路和次短路的长度和方案. 但往优先队列里放的结构体和vis竟然也要区分0/1,就像把一个点拆成两个点了一样. 不要区分k的fx. #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int N=1005,M=10005; int T

poj 3463 Sightseeing(次短路+条数统计)

/* 对dij的再一次理解 每个点依旧永久标记 只不过这里多搞一维 0 1 表示最短路还是次短路 然后更新次数相当于原来的两倍 更新的时候搞一下就好了 */ #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<vector> #define maxn 1010 using namespace std; int T,n,m,num,head[m

(最短路深入)POJ 3463 - Sightseeing

题意: 给一个有向图,计算最短路和比最短路少1的路的条数的和. 分析: 这题真的写死我了. 因为之前很少接触最短路问题,所谓偶尔遇到一次也是套的模板,根本没有细细思考过dijsktra算法.所以栽在了这题上. 这题就是求最短路和次短路. 核心思想在于修改最短路松弛的条件,并且每个节点同时维护最短路和次短路. 很多博主写的很详细,我也不多说了,只是写个博文记录一下自己有多渣,在学习算法的道路上自己真的思考的不够多,也不够努力. 代码: 1 #include <set> 2 #include &l

POJ 3463 - Sightseeing

题意:告诉你那n个点以及m条单向边.询问你从s点到e点最短路和比最短路长度大一的路一共有多少条. 思路:dijkstra变形.分别从起点和终点求一边dijkstra.用cnt数组分别记录从起点到达第i个点且长度为最短长度的路径数以及从原点到达第i个点且长度为最短长度的路径数._cnt数组记录到第i个节点的距离为最短距离+1的路径数.注意_cnt[i]所记录的路径只包括第i个节点的pre节点为最短路而pre节点到第i个节点不是最短路的情况.这样可以防止重复计算. 具体的松弛操作:设u为未经过的节点

POJ 1062 昂贵的聘礼 最短路

Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求.酋长说:"嗯,如果你能够替我弄到大祭司的皮袄,我可以只要8000金币.如果你能够弄来他的水晶球,那么只要5000金币就行了."探险家就跑到大祭司那里,向他要求皮袄或水晶球,大祭司要他用金币来换,或者替他弄来其他的东西,他可以降低价格.探险家于是又跑到其他地方,其他人也提出了类似的要求

POJ 2449 Remmarguts&#39; Date (第k短路 A*搜索算法模板)

Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 22412   Accepted: 6085 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, h