1 Star 0 Fork 0

hsmath/PAT advanced

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
1003.cpp 2.41 KB
一键复制 编辑 原始数据 按行查看 历史
Shuang Hu 提交于 2021-03-22 19:29 . My patest code
#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();
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/hsmath/pat-advanced.git
git@gitee.com:hsmath/pat-advanced.git
hsmath
pat-advanced
PAT advanced
master

搜索帮助