hdu 3191 How Many Paths Are There

时间:2023-03-09 02:56:50
hdu 3191 How Many Paths Are There

http://acm.hdu.edu.cn/showproblem.php?pid=3191

这道题求次短路经和路径数

 #include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define maxn 2000
using namespace std;
const int inf=<<;
struct edge
{
int v,w;
}; struct node
{
int d,v;
int mark;
bool operator < (const node &a)const
{
if(d!=a.d)
return d>a.d;
return v>a.v;
}
}; vector<edge>edges[maxn];
int dis[maxn][];
int vis[maxn][];
int path[maxn][];
int n,m,a,b,c,s1,f;
node st; void inti()
{
for(int i=; i<=n; i++)
{
dis[i][]=dis[i][]=inf;
}
memset(path,,sizeof(path));
memset(vis,false,sizeof(vis));
} void dijkstra(int s,int e)
{
inti();
priority_queue<node>q;
dis[s][]=;
path[s][]=;
memset(vis,false,sizeof(vis));
st.d=;
st.v=s;
st.mark=;
q.push(st);
while(!q.empty())
{
node st1=q.top();
q.pop();
if(vis[st1.v][st1.mark]) continue;
vis[st1.v][st1.mark]=true;
for(int i=; i<(int)edges[st1.v].size(); i++)
{
int v1=edges[st1.v][i].v;
int w1=edges[st1.v][i].w;
if(!vis[v1][]&&st1.d+w1<dis[v1][])
{
if(dis[v1][]!=inf)
{
dis[v1][]=dis[v1][];
path[v1][]=path[v1][];
st.d=dis[v1][];
st.v=v1;
st.mark=;
q.push(st);
}
dis[v1][]=st1.d+w1;
path[v1][]=path[st1.v][st1.mark];
st.v=v1;
st.mark=;
st.d=dis[v1][];
q.push(st);
}
else if(!vis[v1][]&&st1.d+w1==dis[v1][])
{
path[v1][]+=path[st1.v][st1.mark];
}
else if(!vis[v1][]&&st1.d+w1<dis[v1][])
{
dis[v1][]=st1.d+w1;
path[v1][]=path[st1.v][st1.mark];
st.d=dis[v1][];
st.v=v1;
st.mark=;
q.push(st);
}
else if(!vis[v1][]&&st1.d+w1==dis[v1][])
{
path[v1][]+=path[st1.v][st1.mark];
}
}
}
} int main()
{
while(scanf("%d%d%d%d",&n,&m,&s1,&f)!=EOF)
{
inti();
for(int i=; i<=n; i++) edges[i].clear();
for(int i=; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c);
edge m1;
m1.v=b;
m1.w=c;
edges[a].push_back(m1);
}
dijkstra(s1,f);
printf("%d %d\n",dis[f][],path[f][]);
}
return ;
}