pat甲级打卡-1003 Emergency
作者:互联网
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 505;
int dis[N],w[N];
bool visit[N];
int e[N][N],weight[N],num[N];//num 记录到 i 的最短路径条数
int n,m,c1,c2;
const int inf = 99999999;
//target 输出最短路径条数,以及最小点权和
int dijkstra(){
memset(dis ,0x3f,sizeof dis);
dis[c1] = 0;
w[c1] = weight[c1];
num[c1] = 1;
for(int i = 0; i < n; i++) {
int u = -1, minn = inf;
for(int j = 0; j < n; j++) {
if(!visit[j] && (u==-1 || dis[j] < dis[u])) {
u = j;
}
}
minn=dis[u];
visit[u] = true;
for(int v = 0; v < n; v++) {
if(visit[v] == false && e[u][v] != inf) { //连接当前最短路上节点,且未被访问
if(dis[u] + e[u][v] < dis[v]) { //更新与当前最短路节点相连的节点的dis最短路径长
dis[v] = dis[u] + e[u][v];
num[v] = num[u];//条数和当前最短路节点一致
w[v] = w[u] + weight[v];
} else if(dis[u] + e[u][v] == dis[v]) { //边权相等
num[v] = num[v] + num[u]; //更新路径数为两种情况的和,一种是走v之前的几条路径,另一种是走过u当前最短路节点的路径条数
if(w[u] + weight[v] > w[v]) //w[]记录到v结点的最大点权和。
w[v] = w[u] + weight[v];
}
}
}
}
return num[c2];
}
int main(){
memset(e,0x3f,sizeof e);
cin>>n>>m>>c1>>c2;
for(int i=0;i<n;i++){
cin>>weight[i];
}
for(int i=1;i<=m;i++){
int x,y,z;
cin>>x>>y>>z;
e[x][y]=min(e[x][y],z);
e[y][x]=e[x][y];
}
cout<<dijkstra()<<" ";
//for(int i=0;i<n;i++) cout<<dis[i]<<" ";
cout<<w[c2];
return 0;
}
标签:pat,weight,int,条数,num,打卡,1003,c1,dis 来源: https://www.cnblogs.com/rabbithacker/p/16154671.html