代码拉取完成,页面将自动刷新
#include <iostream>
#define MaxVertexNum 505
#define INFINITY 0x7ffffff
using namespace std;
class Map{
private:
int Nv;
int Ne;
int G[MaxVertexNum][MaxVertexNum];
int Pnum[MaxVertexNum];
int origin;
int terminal;
int PathNum;
int MaxPeople;
public:
Map(){
PathNum=0;
MaxPeople=0;
}
void MakeMap();
void ShortestLength();
void print(){
if(Nv==0){
cout<<"0 0"<<endl;
}else if(origin==terminal){
cout<<"1 "<<Pnum[origin]<<endl;
}else{
cout<<PathNum<<" "<<MaxPeople<<endl;
}
}
};
void Map::MakeMap(){
cin>>Nv>>Ne>>origin>>terminal;
for(int i=0;i<Nv;i++){
cin>>Pnum[i];
}
for(int i=0;i<Nv;i++){
for(int j=0;j<Nv;j++){
G[i][j]=-1;//Isn't Connected
}
}
int pathorigin;
int pathterminal;
int pathlength;
for(int i=0;i<Ne;i++){
cin>>pathorigin>>pathterminal>>pathlength;
G[pathorigin][pathterminal]=pathlength;
G[pathterminal][pathorigin]=pathlength;
}
}
void Map::ShortestLength(){
int NowDistNum[MaxVertexNum];
int IsKnown[MaxVertexNum]={0};
int NowPeopleNum[MaxVertexNum]={0};
int NumberOfPath[MaxVertexNum]={0};
int V=-1;
int num=0;
int dist;
int min;
for(int i=0;i<Nv;i++){
NowDistNum[i]=INFINITY;
if(i==origin){
NumberOfPath[i]=1;
NowDistNum[i]=0;
}
}
while(1){
min=900000;
num=0;
V=-1;
for(int i=0;i<Nv;i++){
if(IsKnown[i]==0){
if(NowDistNum[i]<min){
V=i;
min=NowDistNum[i];
num=NowPeopleNum[i];
}
else if(NowDistNum[i]==min&&NowPeopleNum[i]>=num){
V=i;
min=NowDistNum[i];
num=NowPeopleNum[i];
}
}
}
if(V==-1){
break;
}
IsKnown[V]=1;
for(int i=0;i<Nv;i++){
if(!IsKnown[i]){
if(G[V][i]>=0){
dist=NowDistNum[V]+G[V][i];
if(NowDistNum[i]>dist){
NumberOfPath[i]=NumberOfPath[V];
NowDistNum[i]=dist;
NowPeopleNum[i]=NowPeopleNum[V]+Pnum[V];
}
else if(NowDistNum[i]==dist){
NumberOfPath[i]=NumberOfPath[i]+NumberOfPath[V];
num=NowPeopleNum[V]+Pnum[V];
if(num>NowPeopleNum[i]){
NowPeopleNum[i]=num;
}
}
}
}
}
}
PathNum=NumberOfPath[terminal];
MaxPeople=NowPeopleNum[terminal]+Pnum[terminal];
}
int main(){
Map M;
M.MakeMap();
M.ShortestLength();
M.print();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。