您的位置:首页 > 其它

POJ 3463 贪心+最短路劲

2012-07-28 16:01 267 查看
原著:/article/6449065.html

题目:

Description

Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one cityS to another cityF. 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 fromS toF. Therefore, Your Personal Holiday wants to offer its clients a choice from many different routes.
As hotels have been booked in advance, the starting cityS and the final cityF, though, are fixed. Two routes from
S to F are considered different if there is at least one road from a cityA to a cityB 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 fromS toF. 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 toF = 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 cityA to cityB 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 cityA to a cityB.

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.

题目大意:求出原点到终点的最短路劲和比最短路劲长度大1的路径条数之和,举个例子:假如A--->B的最短路劲为3,还有2条路劲从A--->B的长度为4,那么结果就是1+2,输出3。

解题思路:

本题若开始直接在Dijkstra中加入针对加1的递推式, 必须在Dijkstra的过程中加入DFS, 很繁琐. 改换求次短路, 然后判断次短路是否比最短路长1.

解法一、A*求第K短路,把前面K条路都求出来,pku 2449 ,但是据说这个题的最后答案超过10^8,把路全部找出来肯定会爆priority_queue

解法二、

改进Dijkstra算法,。将状态扩展到二维,第一维仍然是顶点编号,第二维的长度为2,分别用于记录最短路和次短路。

这样的数据有两个,dist[][2]记录距离,cnt[][2]计数。

更新状态时:

1)新值小于最短路径长:更新最短路径长,计数;次短路径长,计数

2)新值等于最短路径长:更新最短路径计数

3)新值大于最短路径长,小于次短路径长:更新次短路径长,计数

4)新值等于次短路径长:更新次短路径计数

值得注意的是,题目图有重边,所以不能用邻接矩阵存储。

代码:

虽然解法一不能通过,但是思路还是得明白,下面贴出解法一的代码,给大家参考(注意:解法一是通不过测试的,返回的结果是RE)

//存储结构类似于邻接表
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

int dis[1005];//终点到各节点的距离

struct node
{
int to;
int dis;
};

struct ege
{
int to;
int weight;
bool operator<(const ege&b)const
{
return (dis[to]+weight)>(dis[b.to]+b.weight);
}
};

vector<node>map[1005];//用来存储正向边
vector<node>remap[1005];//存储反向边

int form,to;
int n,m;

void Init()//清理map中的数据
{
int i;
for(i=0;i<1005;i++)
{map[i].clear();remap[i].clear();}
}
void spfa()//求出终点到其他点的最短距离
{
int i;
queue<int>p;
node q;
bool user[1005]={0};
user[to]=1;
p.push(to);
for(i=1;i<=n;i++)dis[i]=1100000000;
dis[to]=0;
while(!p.empty())
{
int u=p.front();
p.pop();
user[u]=0;
for(i=0;i<remap[u].size();i++)
{
q=remap[u][i];
if(dis[q.to]>dis[u]+q.dis)
{
dis[q.to]=dis[u]+q.dis;
if(!user[q.to])
{
user[q.to]=1;
p.push(q.to);
}
}
}
}
}

void astar()
{
int num=1;
priority_queue<ege>p;//创建优先队列
int cnt[1005]={0};
int i,x,len;
ege n1,n2;
n1.to=form;
n1.weight=0;
p.push(n1);
while(!p.empty())
{
n2=p.top();
p.pop();
x=n2.to;
len=n2.weight;
cnt[x]++;
if(cnt[to]==2)//终点进队两次,则找到一条路劲
{
if((len==(dis[form]+1))||(len==dis[form]))
{
num++;
//这里不把“cnt[to]--”这条语句放在if外是因为优先队列的性质
//如果找到的这条路劲比最短路劲长2以上的,那以后的路劲都不符合要求
cnt[to]--;
}
continue;
}
if(cnt[x]>2)
continue;
for(i=0;i<map[x].size();i++)
{
n1.to=map[x][i].to;
n1.weight=len+map[x][i].dis;
p.push(n1);
}
}
cout<<num<<endl;
}
int main()
{
int t,i;
cin>>t;
while(t--)
{
Init();
cin>>n>>m;
node p;
for(i=0;i<m;i++)
{
int a,b,c;
cin>>a>>b>>c;
p.to=b;
p.dis=c;
map[a].push_back(p);
p.to=a;
remap[b].push_back(p);
}
cin>>form>>to;
spfa();
astar();
}
return 0;
}


解法二:DIJ+贪心

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define marray 10001
#define narray 1001
const int maxData = 0x7fffffff;
typedef struct edge
{
int v;
int w;
}edge;
int n,m;
int src,des;
int edgenum;
vector<edge> adj[narray];
bool final[narray][2];//标记是否已经选中过
int d[narray][2];//记录最短路劲和次短路劲的长度
int cnt[narray][2];//记录最短路劲和次短路劲的条数
int Dij()
{
int i,j,k,v;
for(i=1;i<=n;++i)
{
d[i][0] = maxData;
d[i][1] = maxData;
cnt[i][0] = maxData;
cnt[i][1] = maxData;
final[i][0] = false;
final[i][1] = false;
}
cnt[src][0] = 1;
d[src][0] = 0;
//final[src][0] = true; 此处不能将final[src][0]的值赋值为true,因为本题使用邻接表实现,d[i][0],d[i][1]没有初始化为与源点的距离
for(i=1;i<2*n;++i)//i<2n是因为有两条路劲,每次选一个最小值来更新
{
int temp = maxData;
v = -1;
for(j=1;j<=n;++j)
{
if(!final[j][0] && d[j][0]<temp)
{
v = j;
k = 0;
temp = d[j][0];
}
else if(!final[j][1] && d[j][1]<temp)
{
v = j;
k = 1;
temp = d[j][1];
}
}
if(v==-1)
break;
final[v][k] = true;
for(j=0;j<adj[v].size();++j)
{
int u = adj[v][j].v;
int w = adj[v][j].w;
if(d[u][0]>temp+w)
{
d[u][1] = d[u][0];
d[u][0] = temp+w;

cnt[u][1] = cnt[u][0];
cnt[u][0] = cnt[v][k];
}
else if(d[u][0]==temp+w)
{
cnt[u][0]+=cnt[v][k];
}
else if(d[u][1]>temp+w)
{
d[u][1] = temp+w;
cnt[u][1] = cnt[v][k];
}
else if(d[u][1]==temp+w)
{
cnt[u][1] += cnt[v][k];
}

}
}
int num = cnt[des][0];
if(d[des][0]+1==d[des][1])//判断最短路和次短路是否相差一个单位
num += cnt[des][1];
return num;
}
int main()
{
int i,j;
int t;
int start,end,weight;
edge tempedge;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;++i)
{
adj[i].clear();
}
for(i=1;i<=m;++i)
{
scanf("%d%d%d",&start,&end,&weight);
tempedge.v = end;
tempedge.w = weight;
adj[start].push_back(tempedge);
}
scanf("%d%d",&src,&des);
printf("%d\n",Dij());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: