#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 510;
const int INF = 1e9;
int n, m, c1, c2;
int G[maxn][maxn];//边权
int weight[maxn];//点权
bool vis[maxn] = { 0 };
int num[maxn],w[maxn],d[maxn];//分别存储路径条数,最大weight之和以及路径长度
void dijkstra(int s)
{
fill(d, d + maxn, INF);
memset(num, 0, sizeof(num));
memset(w, 0, sizeof(w));
num[s] = 1;
d[s] = 0;
w[s] = weight[s];
for (int i = 0; i < n; i++)
{
int u = -1, min = INF;
for (int j = 0; j < n; j++)
{
if (vis[j] == 0 && d[j] < min)
{
u = j;
min = d[j];
}
}
if (u == -1) return;
vis[u] = 1;
for (int v = 0; v < n; v++)
{
if (vis[v] == 0 && G[u][v] != INF)
{
if (d[u] + G[u][v] < d[v])
{
d[v] = d[u] + G[u][v];
w[v] = w[u] + weight[v];
num[v] = num[u];
}
else if (d[u] + G[u][v] == d[v])
{
if (w[u] + weight[v] > w[v]) w[v] = w[u] + weight[v];
num[v] += num[u];
}
}
}
}
}
int main()
{
int temp,a,b;
fill(G[0], G[0] + maxn * maxn, INF);//把剩余边权填充
scanf("%d%d%d%d", &n, &m, &c1, &c2);
for (int i = 0; i < n; i++)
{
scanf("%d", &weight[i]);
}
for (int i = 0; i < m; i++)
{
scanf("%d%d%d", &a, &b, &temp);
G[a][b] = G[b][a] = temp;
}
dijkstra(c1);
printf("%d %d", num[c2], w[c2]);
return 0;
}
1003 Emergency (25 分)dijkstra算法
最新推荐文章于 2022-01-23 15:54:31 发布