07-图6 旅游规划 (25分)
#include <iostream>
#define MaxSize 501
#define INFINITY 63325
#define ERROR -1
using namespace std;
typedef struct Node
{
int Expense;
int Length;
} MGraph[MaxSize][MaxSize];
MGraph G;
int Collected[MaxSize]={0};
int Dist[MaxSize];
int Cost[MaxSize];
int Nv,Ne;
int Begin,End;
void BuildGraph();
void Dijkstra();
int FindMinDist();
int main()
{
cin>>Nv >>Ne >>Begin >>End;
BuildGraph();
Dijkstra();
cout<<Dist[End]<<" "<<Cost[End];
return 0;
}
void BuildGraph()
{
int V1,V2;
int S,D;
int i,j;
for(i = 0;i<Nv;i++)
{
for(j =0;j<Nv;j++)
{
G[i][j].Expense = INFINITY;
G[i][j].Length = INFINITY;
}
G[i][i].Expense = 0;
G[i][i].Length = 0;
Dist[i] = INFINITY;
Cost[i] = INFINITY;
}
for(i = 0;i<Ne;i++)
{
cin >> V1 >> V2 >> S >> D;
G[V1][V2].Expense = D;
G[V1][V2].Length = S;
G[V2][V1].Expense = D;
G[V2][V1].Length = S;
}
}
void Dijkstra()
{
int V,W;
for(V =0;V<Nv;V++)
{
Dist[V]=G[Begin][V].Length;
Cost[V]=G[Begin][V].Expense;
}
Collected[Begin]=1;
Dist[Begin] = Cost[Begin] = 0;
while(1)
{
V = FindMinDist();
if(V==ERROR) break;
Collected[V] = 1;
for(W = 0;W <Nv; W++)
{
if(Collected[W]==0&&G[V][W].Length<INFINITY)
{
if(Dist[V]+G[V][W].Length<Dist[W])
{
Dist[W] = Dist[V]+G[V][W].Length;
Cost[W] = Cost[V]+G[V][W].Expense;
}else if((Dist[V]+G[V][W].Length==Dist[W])&&(Cost[V]+G[V][W].Expense<Cost[W]))
Cost[W] = Cost[V]+G[V][W].Expense;
}
}
}
}
int FindMinDist()
{
int MinDist = INFINITY;
int MinV=0,i;
for(i=0;i<Nv;i++)
{
if(Collected[i]==0&&Dist[i]<MinDist)
{
MinDist = Dist[i];
MinV = i;
}
}
if(MinDist<INFINITY)
return MinV;
else return ERROR;
}