ZOJ 2526 FatMouse and JavaBean II
博客搬新家,以後就是基本只在自己的獨立博客進行更新,歡迎訪問。http://zihengoi.cn
題目描述:
題目鏈接:ZOJ 2526 FatMouse and JavaBean II
題目大意:
給一個起點和終點。再給一些圖中點的關係路。從起點到終點的最短路徑有多少條。如果存在多條最短路,則求能得到
解題思路:
用
複雜度分析:
時間複雜度 :
空間複雜度 :
AC代碼:
#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
const int maxn = 510;
const int INF = 0x3fffffff;
struct Edge{
int from,to,dist;
};
struct Heapnode{
int d,u;
bool operator < (const Heapnode& rhs) const{
return d > rhs.d;
}
};
bool done[maxn];
vector<Edge> edges;
vector<int> G[maxn];
int n,m,m1,rm1,rm2;
int jb[maxn], sumjb[maxn], d[maxn], p[maxn],ans[maxn],path[maxn];
void dijkstra(int s){
priority_queue<Heapnode>Q;
for(int i = 0; i < n; i++){
d[i] = INF;
sumjb[i] = 0;
ans[i] = 1;
}
memset(done,false,sizeof(done));
for(int i = 0; i < m1; i++) p[i] = s;
d[s] = 0;
sumjb[s] = jb[s];
Q.push((Heapnode){0,s});
while(!Q.empty())
{
Heapnode x = Q.top();
Q.pop();
int u = x.u;
if(done[u]) continue;
done[u] = true;
for(int i = 0; i < G[u].size(); i++)
{
Edge& e = edges[G[u][i]];
if(d[e.to] > d[u] + e.dist)
{
d[e.to] = d[u] + e.dist;
sumjb[e.to] = sumjb[u] + jb[e.to];
ans[e.to] = ans[u];
p[e.to] = G[u][i];
Q.push((Heapnode){d[e.to],e.to});
}
else if(d[e.to] == d[u] + e.dist){
ans[e.to] += ans[u];
if(sumjb[e.to] < sumjb[u] + jb[e.to]){
sumjb[e.to] = sumjb[u] + jb[e.to];
p[e.to] = G[u][i];
Q.push((Heapnode){d[e.to],e.to});
}
}
}
}
}
int main()
{
while(scanf("%d%d%d%d",&n,&m,&rm1,&rm2) != EOF){
int from,to,dist;
for(int i = 0; i < n; i++) G[i].clear();
edges.clear();
m1 = 0;
for(int i = 0; i < n; i++) scanf("%d",&jb[i]);
for(int i = 0; i < m; i++){
scanf("%d%d%d",&from,&to,&dist);
edges.push_back((Edge){from,to,dist});
G[from].push_back(m1++);
edges.push_back((Edge){to,from,dist});
G[to].push_back(m1++);
}
dijkstra(rm1);
printf("%d %d\n",ans[rm2],sumjb[rm2]);
m1 = 1,path[0] = rm2;
while(rm1 != rm2){
path[m1++] = edges[p[rm2]].from;
rm2 = edges[p[rm2]].from;
}
for(int i = m1-1; i > 0; i--)printf("%d ",path[i]);
printf("%d\n",path[0]);
}
return 0;
}