POJ 3463 最短路和次短路的和 dijkstra()

原创 2012年03月21日 10:00:39

    题解:

题目大意:求源点S到终点T的最短路的数量和比最短路长1的数量。

解题思路:我们可以利用dijstra算法的思想,只需在其中进行一些改进即可。可以先定义一个二维的数组dist[N][2]。dist[i][0]代表源点S到点i的最短路,dis[i][1]代表源点S到点i的次短路。初始化dis[S][0]=0,其余的都初始化为无穷大。然后定义一个二维数组count[N][2]记录路径方法数,count[S][0]=1,其余初始为0。再定义一个标记数组vis[N][2],初始vis[S][0]被标记已访问,其余未访问。采用dijstra算法的思想,每次从dis[N][2]中选择一个未被标记且值最小的点dist[v][flag](可能这个值是这个点的最短路,也可能是次短路,只有当此点的最短路被标记了次才可能选中此点的次短路)。再用这个值val去更新此点的邻接点u。更新的方法如下:

(1)如果val小于dist[u][0],则dist[u][1]=dist[u][0],count[u][1]=count[u][0],dist[u][0]=val.count[u][0]=count[v][flag]。否则转(2)

(2)如果val 等于dis[u][0],则count[u][0] += count[v][flag]; 否则转(3)

(3)如果val小于dis[u][1],则dis[u][1]=val.count[u][1]=count[v][flag]。

否则转(4)

(4)如果val等于dis[u][1],则count[u][1] +=count[v][flag].否则什么都不做。

这样循环计算2*n-1次就可以计算出源点到所有点的最短路和次短路的方法数了,而对于终点T,如果次短路比最短路大1则答案为最短路和次短路的方法数之和,否则就为最短路的方法数。

 

对于为什么循环2*n-1次,相当于把每个点拆成了最短路的点,和次短路的点,相当于两个点,每次贪心选择只选择出一个最短路点或者次短路点,所有总共需要选择2*n-1次,即循环2*n-1次

题目:

Sightseeing
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5273   Accepted: 1815

Description

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 AB and L, separated by single spaces, with 1 ≤ AB ≤ NA ≠ 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 ≤ SF ≤ 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
ac代码:

#include <iostream>
#include <string.h>
#include <cstdio>
#include <queue>
using namespace std;
const int N=1010;
const int M=10010;
#define MAX 0x7fffffff
int dis[N][2],visted[N][2],head[N],sum[N][2];
int numcity,numpath,sp,ep,num=0;
struct edge{
  int rp,value,next;
}ee[M];
void addedge(int x,int y,int v){
	ee[num].rp=y;
	ee[num].value=v;
	ee[num].next=head[x];
	head[x]=num++;
}
void dijkstra(){
	for(int i=0;i<=numcity;++i){
	  for(int j=0;j<2;++j)
		  dis[i][j]=MAX;
	}
  dis[sp][0]=0;
  sum[sp][0]=1;
  //printf("sss\n");
  memset(visted,0,sizeof(visted));
  for(int i=1;i<=2*numcity;++i){
	  //printf("i====%d\n",i);
	  int temp=MAX,flag=0,mark=0;
	  for(int j=1;j<=numcity;++j){
		//  printf("j==%d\n",j);
		  if(!visted[j][0]&&dis[j][0]<temp){
		    temp=dis[j][0];
			mark=j;
			flag=0;
		  }
		  else if(!visted[j][1]&&dis[j][1]<temp){
		    temp=dis[j][1];
			mark=j;
			flag=1;
		  }
	  }
	  visted[mark][flag]=1;
	  //printf("aaa\n");
	  for(int k=head[mark];k!=-1;k=ee[k].next){
		  int y=ee[k].rp;
		  int vv=ee[k].value;
		  if(temp+vv<dis[y][0]){
		    dis[y][1]=dis[y][0];
			sum[y][1]=sum[y][0];
			dis[y][0]=temp+vv;
			sum[y][0]=sum[mark][flag];
		  }
		  else if(temp+vv==dis[y][0]){
		    sum[y][0]+=sum[mark][flag];
		  }
		  else if(temp+vv<dis[y][1]){
		    dis[y][1]=temp+vv;
			sum[y][1]=sum[mark][flag];
		  }
		  else if(temp+vv==dis[y][1]){
		    sum[y][1]+=sum[mark][flag];
		  }
		//  printf("sum[0]==%d  sum[1]==%d\n",sum[y][0],sum[y][1]);
	  }
	  //printf("sssss\n");
  }
}
int main(){
  //freopen("in.txt","r",stdin);
  int numcase;
  scanf("%d",&numcase);
  while(numcase--){
    scanf("%d%d",&numcity,&numpath);
	num=0;
	memset(head,-1,sizeof(head));
	memset(sum,0,sizeof(sum));
	int x,y,v;
	while(numpath--){
	  scanf("%d%d%d",&x,&y,&v);
	  addedge(x,y,v);
	}
	scanf("%d%d",&sp,&ep);
	dijkstra();
	//printf("dis[ep][1]==%d  dis[ep][0]==%d\n",dis[ep][1],dis[ep][0]);
	//printf("sum[1]==%d  sum[0]==%d\n",sum[ep][1],sum[ep][0]);
	if(dis[ep][1]-1==dis[ep][0])
		printf("%d\n",sum[ep][1]+sum[ep][0]);
	else
		printf("%d\n",sum[ep][0]);
  }
  return 0;
}


Dijkstra求最短路与次短路

花了一个晚上加上午两节课的时间来思考这个问题,一开始进了个误区,后来发现不对,找到了正确思路,现在来做个总结 最短路: 假设有如下 无向图: 每条边有权,要求从A到G的最短路,设数组d[i]用来记...
  • qq_25971709
  • qq_25971709
  • 2015年04月28日 12:52
  • 680

poj3255 次短路Dijkstra

挑战程序设计竞赛上的题目。 题意:给一个边权都是正的无向图,求1到n点的次短路。 分析: 先说一下标准的dijkstra+heap的,实际上以前写的程序都是在完整循环一次之后会确定从s到这个点的最短路...
  • NGccc
  • NGccc
  • 2015年04月15日 16:54
  • 572

poj 3463 最短路和次短路

题意:n个点,m条有向边,给定起点和终点,求出起点到终点最短路和比最短路长1的路径的总条数 dijkstra算法,邻接表建图 改进Dijkstra算法。将状态扩展到二维,第一维仍然是顶点编号,第二...
  • Eirlys_North
  • Eirlys_North
  • 2017年02月28日 19:53
  • 137

Sightseeing_poj3463_dijkstra

DescriptionTour operator Your Personal Holiday organises guided bus trips across the Benelux. Every ...
  • jpwang8
  • jpwang8
  • 2016年09月16日 12:31
  • 207

POJ 3463 Sightseeing【次短路,Dijkstra算法,链式前向星建图】

题意:旅行团每天固定的从S地出发到达T地,为了省油要求尽量走最短路径或比最短路径长1单位距离的路径,求满足条件的路径条数 算法:最短路和次短路。Dijkstra算法。采用邻接表建图。 总结:不要用...
  • hurmishine
  • hurmishine
  • 2016年08月26日 10:53
  • 779

POJ 3463 最短路 次短路

本题是求最短路和比最短路距离长1的次短路的个数,于是就用到了dijkstra 主要的改变就是数组都开到了二维,第二维用来表示是最短路还是次短路 比如d[][]数组和vis[][]数组 而cnt数...
  • sdj222555
  • sdj222555
  • 2012年06月25日 18:14
  • 3085

3463 Sightseeing dijkstra求最短路和次短路

SightseeingTime Limit: 2000MS Memory Limit: 65536KTotal Submissions: 2864 Accepted: 949DescriptionTo...
  • kongming_acm
  • kongming_acm
  • 2010年07月17日 15:04
  • 801

poj 3463 dijkstra变形(求最短路和次短路的数量)

题意:给定一个带权有向图以及起点s和终点t,
  • dumeichen
  • dumeichen
  • 2014年10月07日 20:21
  • 369

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

Language: Default Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Su...
  • u011699990
  • u011699990
  • 2015年05月14日 01:08
  • 481

Sightseeing POJ - 3463 求最短路和次短路

题意: 给出一个n点m边的有向图,求s到t的最短路和长度为最短路+1的路的种类数; n...
  • yanga11ang
  • yanga11ang
  • 2017年10月06日 15:36
  • 59
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 3463 最短路和次短路的和 dijkstra()
举报原因:
原因补充:

(最多只允许输入30个字)