// 记录当前点到原点的路径开销
bool visited[1001]; // 1000+1 是为去除下标0 , 直接从1 开始
list<edge> Vertex[1001]; // 当前节点到目标节点的权重
priority_queue<int, vector<pii>, greater<pii> > pq; // 这是最小堆
void readData()
{
int f, // 前向的顶点
t, // 前向顶点所指向的目标顶点
w; // 以上两点路径所花的权重
int fTest[5] = {1, 2, 3, 4, 1}; // 免输入测试数据
int tTest[5] = {2, 3, 4, 5, 5};
int wTest[5] = {20, 30, 20, 20, 100};
for(int i = 0; i < T; i++)
{
//scanf("%d%d%d", &f, &t, &w);
f = fTest[i];
t = tTest[i];
w = wTest[i];
printf("%d %d %d\n", f, t, w);
Vertex[f].push_front(make_pair(t, w)); // 存的是前向顶点的出度
Vertex[t].push_front(make_pair(f, w)); // 存的是目标顶点的入度
}
}
void init()
{
for(int i = 1; i <= N; i++)
{
totalCostToThisVertex[i] = INF; // 无穷远, 表示不可连通
visited[i] = false; // 初始一个未访问数组
}
totalCostToThisVertex[N] = 0; // 起点开销为 0
// 这道题要从 landmark[5] 赶回 landmark[1]
pq.push(make_pair(totalCostToThisVertex[N], N)); // 第一个入队列的顶点
}
void dijkstra()
{
while(!pq.empty()) // 查看优先级队列是否为空
{
pii u = pq.top(); // 记录队头的元素
pq.pop(); // 队头出队, 以此来推进当前顶点
int currentVertex = u.second; // 记录对头顶点 ,也就是开销最少的路径
if(visited[currentVertex])
{
continue;
}// 如果顶点已经访问过就掠过进入下一个循环
visited[currentVertex] = true; // 标记为访问过
list<edge> es = Vertex[currentVertex]; // 当前顶点
list<edge>::iterator it;
printf("checking %d's edges\n", currentVertex);
for(it = es.begin(); it != es.end(); it++)
{ // 遍历当前顶点的出度
int targetVertex = it->first;
printf("checking %d\n", targetVertex);
// 松弛技术
if (totalCostToThisVertex[targetVertex] >
totalCostToThisVertex[currentVertex] + it->second)
{
totalCostToThisVertex[targetVertex] =
totalCostToThisVertex[currentVertex] + it->second;
pq.push(
make_pair(
totalCostToThisVertex[targetVertex],
targetVertex
) //
);
}// end if
}// end for
}// end while
printf("%d\n", totalCostToThisVertex[1]); // 打印出从起点Landmarks[5] 到 Landmarks[1] 最短路径的开销
}
int main()
{
//scanf("%d%d", &T, &N);
T = 5; // 免输入测试数据
N = 5;
readData();
init();
dijkstra();
getchar();
}
/*
Til the Cows Come Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 16537 Accepted: 5461
Description
Bessie
is out in the field and wants to get back to the barn to get as much
sleep as possible before Farmer John wakes her for the morning milking.
Bessie needs her beauty sleep, so she wants to get back as quickly as
possible.
Farmer John's field has N (2 <= N <= 1000) landmarks in it,
uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in
which Bessie stands all day is landmark N. Cows travel in the field
using T (1 <= T <= 2000) bidirectional cow-trails of various
lengths between the landmarks. Bessie is not confident of her navigation
ability, so she always stays on a trail from its start to its end once
she starts it.
Given the trails between the landmarks, determine the minimum
distance Bessie must walk to get back to the barn. It is guaranteed that
some such route exists.
Input
* Line 1: Two integers: T and N
*
Lines 2..T+1: Each line describes a trail as three space-separated
integers. The first two integers are the landmarks between which the
trail travels. The third integer is the length of the trail, range
1..100.
Output
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
Hint
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
*/
Reference:
1.最短路径算法—Dijkstra(迪杰斯特拉)算法分析与实现(C/C++) <-
http://www.wutianqi.com/?p=1890
2.Til the Cows Come Home <-
http://poj.org/problem?id=2387