反思:
1.priority_queue默认会优先输出大的元素,所以要注意重载运算符。
2.一定要记得resize
3.重载运算符标准方式
bool operator < (CNode b) const
{
return w > b.w;
}
#include <iostream>
#include <queue>
#include <cstdio>
#include <map>
#include <cstring>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
struct CNode
{
int v;
int w;//到源点的距离
bool operator < (CNode b) const
{
return w > b.w;
}
CNode(){}
CNode(int vv, int ww)
{
v = vv;
w = ww;
}
};
int t, n;
priority_queue<CNode> q;
vector<vector<CNode> > G;
int bUsed[1010];
void Init()
{
memset(bUsed, 0, sizeof(bUsed));
}
void Dijkstra()
{
q.push(CNode(n, 0));
while (!q.empty()) {
CNode tmp;
tmp = q.top();
q.pop();
if (bUsed[tmp.v]) continue;
bUsed[tmp.v] = 1;
if (tmp.v == 1) {
printf("%d\n", tmp.w);
break;
}
for (int i = 0; i < G[tmp.v].size(); i++) {
CNode t = G[tmp.v][i];
if (bUsed[t.v]) continue;
q.push(CNode (t.v, tmp.w + t.w));
}
}
return;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
scanf("%d%d", &t, &n);
G.resize(n + 1);
for (int i = 0; i < t; i++) {
int u, v, w;
scanf ("%d%d%d", &u, &v, &w);
G[u].push_back(CNode(v, w));
G[v].push_back(CNode(u, w));
}
Init ();
Dijkstra ();
return 0;
}