POJ 2387 Til the Cows Come Home
题意:给一个无向图,求从结点n到结点1的最短路
思路:裸的Dijkstra……邻接矩阵的话就在原来有向图的基础上多一次AddEdge操作就完事了
const int INF = 100000+1;
const int maxn = 1000+10;
struct node {
LL d;
int u;
bool operator < (const node& k) const {
return d > k.d;
}
};
struct Edge {
int from, to;
LL dis;
Edge(int u,int v,int d):from(u),to(v),dis(d){}
};
int n, m;
vector<Edge> Edges;
vector<int> G[maxn];
bool done[maxn];
LL d[maxn];
void solve(){
cin >> m >> n;
memset(done, false,sizeof(done));
for (int i = 1; i <= n; i++) d[i] = (i == n ? 0 : INF);
Edges.push_back(Edge(0, 0, 0));
int num = 0;
for (int i = 1; i <= m; i++) {
int u, v, d;
cin >> u >> v >> d;
Edges.push_back(Edge(u, v, d));
G[u].push_back(++num);
Edges.push_back(Edge(v, u, d));
G[v].push_back(++num);
}
priority_queue<node> Q;
Q.push(node{ 0, n });
while (!Q.empty()) {
node x = Q.top(); Q.pop();
int u = x.u;
if (done[u]) continue;
for (int i = 0; i < G[u].size(); i++) {
Edge& e = Edges[G[u][i]];
if (d[u] + e.dis < d[e.to]) {
d[e.to] = d[u] + e.dis;
Q.push(node{ d[e.to],e.to });
}
}
done[u] = true;
}
cout << d[1];
}