关闭

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

标签: distanceinputoutputeach算法file
1643人阅读 评论(0) 收藏 举报
分类:

    题解:

题目大意:求源点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;
}


2
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:612263次
    • 积分:10792
    • 等级:
    • 排名:第1491名
    • 原创:414篇
    • 转载:2篇
    • 译文:0篇
    • 评论:119条
    最新评论