http://acm.hdu.edu.cn/showproblem.php?pid=3191
oooccc1 is a Software Engineer who has to ride to the work place every Monday through Friday. For a long period, he went to office with the shortest path because he loves to sleep late…Time goes by, he find that he should have some changes as you could see, always riding with the same path is boring.
One day, oooccc1 got an idea! Why could I take another path? Tired at all the tasks he got, he got no time to carry it out. As a best friend of his, you’re going to help him!
Since oooccc1 is now getting up earlier, he is glad to take those paths, which are a little longer than the shortest one. To be precisely, you are going to find all the second shortest paths.
You would be given a directed graph G, together with the start point S which stands for oooccc’1 his house and target point E presents his office. And there is no cycle in the graph. Your task is to tell him how long are these paths and how many there are.
Input
There are some cases. Proceed till the end of file.
The first line of each case is three integers N, M, S, E (3 <= N <= 50, 0 <= S , E <N)
N stands for the nodes in that graph, M stands for the number of edges, S stands for the start point, and E stands for the end point.
Then M lines follows to describe the edges: x y w. x stands for the start point, and y stands for another point, w stands for the length between x and y.
All the nodes are marked from 0 to N-1.
Output
For each case,please output the length and count for those second shortest paths in one line. Separate them with a single space.
Sample Input
3 3 0 2 0 2 5 0 1 4 1 2 2
Sample Output
6 1
题目大意:求从指定起点到指定终点的次短路径(第二短)的长度和路径数。
思路:cnt[i][1]表示到达点i最短的路有多少条,cnt[i][2]表示次短的条数;dist[i][1]表示到达点i最短路的长度,dist[i][2]表示次短路的长度 用v去松驰u时有四种情况 (设当前dist[v][cas]) 情况1:dist[v][cas]+w(v,u)<dist[u][1],找到一个更短的距离,则把原来最短的距离作为次短的距离,同时更新最短的.即 dist[u][2]=dist[u][1] dist[u][1]=dist[v][cas]+w(v,u); cnt[u][2]=cnt[u][1] ; cnt[u][1]=cnt[v][cas]; 把Node(dist[u][1],u,1)和Node(dist[u][2],u,2)放入队列 情况2:dist[v][cas]+w(v,u)==dist[u][1],找到一条新的相同距离的最短路,则cnt[u][1]+=cnt[v][cas],其他不用更新,也不入队 情况3:dist[v][cas]+w(v,u)<dist[u][2],不可以更新最短距离,但可以更新次短的,则更新dist[u][2]和dp[u][2],dist[u][2]=dist[v][cas]+w(v,u); cnt[u][2]=dp[v][cas]; 把Node(dist[u][2],u,2)放入队列 情况4:dist[v][cas]+w(v,u)==dist[u][2] 找到一条新的相同距离的次短路,则cnt[u][2]+=cnt[v][cas],其他不更新。 跑dijkstra即可。慢慢消化吧。
#include<iostream>
#include<cstdio>
#include<stack>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<set>
#include<algorithm>
#include<iterator>
#define INF 0x3f3f3f3f
#define EPS 1e-10
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int maxn=100;
struct edge
{
int to; //尾结点
int v; //权值
};
struct node
{
int bh; //结点编号
int dis; //距离
int flag; //1标记最短路 2标记次短路
};
bool operator <(node a,node b)
{
if(a.dis==b.dis)
return a.bh>b.bh;
return a.dis>b.dis;
}
vector<edge> vec[maxn];
int n,m,s,e;
int dist[maxn][3]; //存储到某个点的最短路径和次短路经的距离
int cnt[maxn][3]; //存储到某个点的最短路径和次短路经的条数(方法数)
int vis[maxn][3]; //标记某个点是否走过 最短路和次短路都需要标记
void Dijkstra();
int main()
{
while(~scanf("%d%d%d%d",&n,&m,&s,&e))
{
for(int i=0;i<n;i++)
vec[i].clear(); //清空上一次存储的边
for(int i=1;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
edge E;
E.to=v,E.v=w; //尾结点和权值
vec[u].push_back(E); //把边存储到对应的头结点下
}
Dijkstra();
printf("%d %d\n",dist[e][2],cnt[e][2]);
}
return 0;
}
void Dijkstra()
{
memset(dist,INF,sizeof(dist)); //存储最短距离的数组附初始值
memset(cnt,0,sizeof(cnt)); //方案数组置零
memset(vis,0,sizeof(vis)); //标记数组置零
priority_queue<node> q;
node p;
p.bh=s,p.dis=0,p.flag=1; //最短路起始结点
cnt[s][1]=1; //最短路径数
dist[s][1]=0; //最短距离
q.push(p);
while(!q.empty())
{
node nd1=q.top();
q.pop();
if(vis[nd1.bh][nd1.flag]) //该结点已经访问过
continue;
vis[nd1.bh][nd1.flag]=1; //置标志位
for(int i=0;i<vec[nd1.bh].size();i++)//遍历以该结点为头结点的边
{
int next=vec[nd1.bh][i].to; //该边尾结点
int value=vec[nd1.bh][i].v; //该边权值
if(nd1.dis+value<dist[next][1]) //发现新的最短路径 更新
{
if(dist[next][1]!=INF) //最短路径存在时更新次短路径 否则可能重合了
{
node temp;
temp.bh=next; //当前最短路径->次短路径
temp.dis=dist[next][1];
temp.flag=2; //标记置2
dist[next][2]=dist[next][1];//更新次短路径的距离
cnt[next][2]=cnt[next][1];//更新次短路径的条数
q.push(temp); //结点入队
}
node temp; //更新当前最短路径
temp.bh=next; //新结点的编号
temp.dis=nd1.dis+value; //最短距离
temp.flag=1; //标志位
q.push(temp);
dist[next][1]=nd1.dis+value;//更新最短路径距离
cnt[next][1]=cnt[nd1.bh][nd1.flag];//因为并不确定目前的结点 是最短路的还是次短路的
}
else if(nd1.dis+value==dist[next][1])//找到一条跟目前最短路径距离相等的 更新方法数即可
cnt[next][1]+=cnt[nd1.bh][nd1.flag];
else if(nd1.dis+value<dist[next][2]) //找到一条更优的次短路径
{
dist[next][2]=nd1.dis+value;
cnt[next][2]=cnt[nd1.bh][nd1.flag];
node temp;
temp.dis=nd1.dis+value;
temp.bh=next;
temp.flag=2;
q.push(temp);
}
else if(nd1.dis+value==dist[next][2])//找到一条跟目前次短路径距离相等的 更新方法数
cnt[next][2]+=cnt[nd1.bh][nd1.flag];
}
}
}