-
时间:2016-04-02 10:34:36 星期六
-
题目编号:[2016-04-02][POJ][2387][Til the Cows Come Home]
-
题目大意:给定n个节点和t条路,求n到1的最短路长度
-
分析:跑一次最短路即可
-
遇到的问题:
- 据说是多重边,如果是用邻接矩阵的就要更新最小值,
- 此题是先输入t,再输入n,输入的时候读错,无限WA…
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 1000 + 10;
const int maxe = 2000 + 10;
struct mNode{
int u,c;
mNode(int _u = 0,int _c = 0):u(_u),c(_c){}
bool operator < (const mNode & a)const{
return c > a.c;
}
};
struct Edge{
int v,c;
Edge(int _v = 0,int _c = 0):v(_v),c(_c){}
};
vector<Edge> e[maxe];
bool vis[maxn];
int d[maxn];
void Dijkstra(int s){
memset(vis,0,sizeof(vis));
memset(d,0x3f,sizeof(d));
priority_queue<mNode> q;
d[s] = 0;
q.push(mNode(s,0));
mNode tmp;
while(!q.empty()){
tmp = q.top();
q.pop();
int u = tmp.u;
if(vis[u]) continue;
vis[u] = 1;
for(int i = 0;i < e[u].size();++i){
Edge & me = e[u][i];
if(!vis[me.v] && d[me.v] > d[u] + me.c){
d[me.v] = d[u] + me.c;
q.push(mNode(me.v,d[me.v]));
}
}
}
}
inline void addedge(int u,int v,int c){
e[u].push_back(Edge(v,c));
}
int main(){
int n,t,a,b,c;
scanf("%d%d",&t,&n);
for(int i = 0;i < t; ++i){
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
addedge(b,a,c);
}
Dijkstra(n);
printf("%d\n",d[1]);
return 0;
}
时间: 2024-10-20 19:17:09