Problem C: Shortest Path
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1007 Solved: 348
[ Submit][ Status][ Web Board]
Description
Abrao is a beautiful country. It has N cities (1 ≤ N≤ 2,500) and C (1 ≤ C ≤ 10,000) highways to connect adjacent cities. Each city is assigned with a unique number from 1 to N. You are required to calculate the length of the shortest path from one city to another.
Input
The first line contains four integers N, C, S and T. N and C has been defined above. S and T (1 ≤ S,T≤ 2,500) are the numbers of two cities. The information about the highways is defined in the following C lines, each one of which contains three integers vi,vj and w indicating a high way between vi and vj with the distance equal to w (there may be multiple highways between two cities). You are required to calculate the length of the shortest path from the city S to T.
Output
The output is the length of the shortest path from the city S to T.
Sample Input
Sample Output
#include <iostream>
#include <vector>
#include <limits.h>
using namespace std;
#define UNVISITED false
#define VISITED true
class Edge
{
private:
int destination;
int weight;
public:
Edge(int a, int b)
{
destination = a;
weight = b;
}
Edge()
{
destination = (int)INT_MAX;
weight = (int)INT_MAX;
}
int Getdestination()
{
return destination;
}
int Getweight()
{
return weight;
}
};
class graph
{
private:
int numofvertices;
int numofhighways;
vector<Edge>* graphvertices;
bool* mask;
public:
graph(int numver, int numhigh)
{
numofvertices = numver;
numofhighways = numhigh;
graphvertices = new vector<Edge>[numver];
mask = new bool[numver];
for (int i = 0; i < numver; i++)
{
mask[i] = UNVISITED;
}
}
void Insert(int start, int destination, int weight)
{
Edge tmp(destination, weight);
graphvertices[start - 1].push_back(tmp);
}
vector<Edge> Getverticelist(int i)
{
return graphvertices[i];
}
int Getnumofvertices()
{
return numofvertices;
}
int Getnumofhighways()
{
return numofhighways;
}
bool Getmask(int i)
{
return mask[i];
}
void Setmask(int i, bool change)
{
mask[i] = change;
}
};
int Minvertice(graph* G, int* D)
{
int min = INT_MAX;
int v = 0;
for (int i = 0; i < G->Getnumofvertices(); i++)
{
if (G->Getmask(i) == UNVISITED&&D[i] < min)
{
v = i;
min = D[i];
}
}
return v;
}
void djkstra(graph* G, int* D, int s)
{
for (int i = 0; i < G->Getnumofvertices(); i++)
{
D[i] = INT_MAX;
}
D[s - 1] = 0;
for (int i = 0; i < G->Getnumofvertices(); i++)
{
int v = Minvertice(G, D);
G->Setmask(v, VISITED);
for (int i = 0; i < G->Getverticelist(v).size(); i++)
{
int w = G->Getverticelist(v).at(i).Getdestination() - 1;
if (D[w] >(D[v] + G->Getverticelist(v).at(i).Getweight()))
{
D[w] = (D[v] + G->Getverticelist(v).at(i).Getweight());
}
}
}
}
int main()
{
int numcities, numhighway, Start, Terminal;
cin >> numcities >> numhighway >> Start >> Terminal;
graph* path = new graph(numcities, numhighway);
int* D = new int[numcities];
while (numhighway--)
{
int start, destination, weight;
cin >> start >> destination >> weight;
path->Insert(start, destination, weight);
path->Insert(destination, start, weight);
}
djkstra(path, D, Start);
cout << D[Terminal - 1] << endl;
return 0;
}
经验:尽量使用c++库内包含的数据结构,自己写链表还有队列之类的会很崩溃的,而且浪费时间又做不好。