题目意思就是要求s到t的最短路径个数和s到t的次短路径个数。
#include<iostream>
#include<string.h>
#include<queue>
#include<stdio.h>
using namespace std;
#define inf 0x3f3f3f3f
int n,m,en,st,et;
int head[1010];
int dis[1010][2];
int cnt[1010][2];
int vis[1010][2];
struct edge
{
int from,to;
int dis;
int next;
}edge[10005];
struct node
{
int v;
int flag;
node(int a,int b)
{
v=a;
flag=b;
}
};
struct cmp
{
bool operator()(struct node a,struct node b)
{
return dis[a.v][a.flag]>dis[b.v][b.flag]; //表示按结构体变量里的x成员为比较要素,升序排列
}
};
int dijkstra()
{
memset(vis,0,sizeof(vis));
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=n;i++)
dis[i][0]=dis[i][1]=inf;
priority_queue<struct node,vector<struct node>,cmp>q;
q.push(node(st,0));
dis[st][0]=0;
cnt[st][0]=1;
while(!q.empty())
{
node top=q.top();
q.pop();
int v=top.v;
int flag=top.flag;
if(vis[v][flag])
continue;
vis[v][flag]=1;
for(int i=head[v];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
int dist=dis[v][flag]+edge[i].dis;
if(dist<dis[to][0])
{
if(dis[to][0]!=inf)
{
dis[to][1]=dis[to][0];
cnt[to][1]=cnt[to][0];
q.push(node(to,1));
}
dis[to][0]=dist;
cnt[to][0]=cnt[v][flag];
q.push(node(to,0));
}
else
if(dist==dis[to][0])
cnt[to][0]+=cnt[v][flag];
else
if(dist<dis[to][1])
{
dis[to][1]=dist;
cnt[to][1]=cnt[v][flag];
q.push(node(to,1));
}
else
if(dist==dis[to][1])
{
cnt[to][1]+=cnt[v][flag];
}
}
}
if(dis[et][0]+1==dis[et][1])
return cnt[et][0]+cnt[et][1];
return cnt[et][0];
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
en=0;
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
edge[en].from=a;
edge[en].to=b;
edge[en].dis=c;
edge[en].next=head[a];
head[a]=en;
en++;
}
scanf("%d%d",&st,&et);
printf("%d
",dijkstra());
}
return 0;
}