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;
}


状态压缩or 最短路 poj2686

自己居然做出来了。用状态压缩的。。 但是用djk却是MLE 啊,然后改了之后是TLE。。。 这用dp是因为这个DAG 状态dp #include #include #include #incl...
  • now_ing
  • now_ing
  • 2017年04月22日 20:16
  • 58

PKU3463(Sightseeing)-最短路和次短路问题,dijkstra算法

最短路和次短路问题,dijkstra算法
  • Jarily
  • Jarily
  • 2013年09月26日 13:05
  • 2909

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

题目链接:http://poj.org/problem?id=3463 题意:求最短路和次短路的总个数,满足次短路的距离+1=最短路。 #include #include #i...

【Dijkstra+邻接表求次短路】POJ Sightseeing 3463

Language: Default Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 77...
  • ydd97
  • ydd97
  • 2015年08月24日 00:37
  • 2109

最短路练习10/poj/1511 Invitation Cards ,(两次spfa),(单源最短路,优先队列优化的Dijkstra)

题目链接:http://poj.org/problem?id=1511 Invitation Cards Time Limit: 8000MS   Memory Lim...

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

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

POJ3463 Sightseeing——最短路的信息附带

题目链接点击打开链接 题意:求最短路和比最短路大1

poj 3463 计算最短与次短路径数

PS:可以用于剖析最短路的算法原理 计算最短路与次短路路径数,次短路长度为最短路径+1 方法记录最短路径与次短路径,并判断次短路径与最短路径之差是否唯一 方法一:反向spfa + dfs(剪枝)...

POJ 3463 Sightseeing Dijkstra最短路&最短路-1的路径数(计数)

题目大意:求给定的DAG中最短路&z
  • Orzmyk
  • Orzmyk
  • 2014年08月25日 21:49
  • 339

图结构_最短路_Dijkstra(单源最短路,不能有负边)

数据结构:邻接表Map 算法功能:求一点到其他点的最短路径,不能有负边 代码算法思想: int Dijkstra(int st,int en,int Map[maxn][maxn],int num)/...
  • cbcbcbz
  • cbcbcbz
  • 2017年03月16日 16:09
  • 133
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 3463 最短路和次短路的和 dijkstra()
举报原因:
原因补充:

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