题目链接<http://poj.org/problem?id=2387>
题意&题解:
先一共有n个点,输入T条边,求从点1到点n的最短路。
注意:1、数据量较大,不适宜用Floyd。
2、会有重边(链式前向星可忽略这点)。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <string.h>
#include <string>
#include <map>
using namespace std;
typedef long long LL;
const int INF=(int)1e9;
int t,n;
struct Node{
int id,v;
Node(int id,int v):id(id),v(v){}
bool operator<(const Node a)const{
return this->v>a.v;
}
};
struct Edge{
int to,val,next;
Edge(int to=0,int val=0,int next=0):to(to),val(val),next(next){}
}e[4005];
int head[1005],now=-1;
void add(int a,int b,int c){
e[++now]=Edge(b,c,head[a]),head[a]=now;
e[++now]=Edge(a,c,head[b]),head[b]=now;
}
int dis[1005],vis[1005];
void dij(int x){
memset(vis,0,sizeof(vis));
memset(dis,125,sizeof(dis));
priority_queue<Node>q;
q.push(Node(x,0));dis[x]=0;
while(!q.empty()){
int u=q.top().id;q.pop();
vis[u]=1;
for(int i=head[u];~i;i=e[i].next){
int v=e[i].to;
if(!vis[v]&&dis[v]-dis[u]>e[i].val){
dis[v]=dis[u]+e[i].val;
q.push(Node(v,dis[v]));
}
}
}
}
int main(){
cin>>t>>n;
memset(head,-1,sizeof(head));
int u,v,w;
while(t--){
cin>>u>>v>>w;
add(u,v,w);
}
dij(1);
cout<<dis[n]<<endl;
}