Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 38556 | Accepted: 13104 |
Description
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
* 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
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
注意:先输入边数后输入结点数,存在重边
#include"cstdio"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
int mp[MAXN][MAXN];
int V,E;
int vis[MAXN];
int d[MAXN];
int dijkstra(int s)
{
for(int i=;i<=V;i++)
{
vis[i]=;
d[i]=mp[s][i];
}
vis[s]=; for(int i=;i<=V;i++)
{
int mincost,k;
mincost=INF;
for(int j=;j<=V;j++)
{
if(!vis[j]&&d[j]<mincost)
{
k=j;
mincost=d[j];
}
} vis[k]=;
for(int j=;j<=V;j++)
{
if(!vis[j]&&d[j]>d[k]+mp[k][j])
{
d[j]=d[k]+mp[k][j];
}
} }
return d[];
}
int main()
{
while(scanf("%d%d",&E,&V)!=EOF)
{
for(int i=;i<=V;i++)
for(int j=;j<=V;j++)
if(i==j) mp[i][j]=;
else mp[i][j]=INF;
for(int i=;i<E;i++)
{
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
if(cost<mp[u][v]) mp[u][v]=mp[v][u]=cost;//存在重边
}
int ans=dijkstra(V);
printf("%d\n",ans);
}
return ;
}
堆优化的dijkstra
#include"cstdio"
#include"vector"
#include"queue"
using namespace std;
typedef pair<int,int> P;
const int MAXN=;
const int INF=0x3fffffff;
int mp[MAXN][MAXN];
int V,E;
vector<int> G[MAXN];
int d[MAXN];
void dijkstra(int s,int end)
{
for(int i=;i<=V;i++) d[i]=INF; priority_queue<P, vector<P>,greater<P> > que;
que.push(P(,s));
d[s]=; while(!que.empty())
{
P p=que.top();que.pop();
if(p.second==end)
{
printf("%d\n",p.first);
return ;
}
int v=p.second;
if(d[v]<p.first) continue;
for(int i=;i<G[v].size();i++)
{
int to=G[v][i];
if(d[to]>d[v]+mp[v][to])
{
d[to]=d[v]+mp[v][to];
que.push(P(d[to],to));
}
}
}
}
int main()
{
while(scanf("%d%d",&E,&V)!=EOF)
{
for(int i=;i<=V;i++)
{
G[i].clear();
for(int j=;j<=V;j++)
if(i==j) mp[i][j]=;
else mp[i][j]=INF;
}
for(int i=;i<E;i++)
{
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
G[v].push_back(u);
G[u].push_back(v);
if(cost<mp[u][v]) mp[v][u]=mp[u][v]=cost;
}
dijkstra(,V);
}
return ;
}
spfa+前向星可解决重边问题。
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN=;
const int INF=0x3f3f3f3f;
struct Edge{
int to,w,next;
}es[];
int head[MAXN],tot;
int n,m;
void addedge(int u,int v,int w)
{
es[tot].to=v;
es[tot].w=w;
es[tot].next=head[u];
head[u]=tot++;
}
int d[MAXN],vis[MAXN];
void spfa(int s)
{
for(int i=;i<=n;i++)
{
d[i]=INF;
vis[i]=;
}
queue<int> que;
que.push(s);
d[s]=;
vis[s]=;
while(!que.empty())
{
int u=que.front();que.pop();
vis[u]=;
for(int i=head[u];i!=-;i=es[i].next)
{
Edge e=es[i];
if(d[e.to]>d[u]+e.w)
{
d[e.to]=d[u]+e.w;
if(!vis[e.to])
{
que.push(e.to);
vis[e.to]=;
}
}
}
}
printf("%d\n",d[n]);
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(head,-,sizeof(head));
tot=;
for(int i=;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
spfa();
}
return ;
}
Java版:
前向星+spfa
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Queue;
class Edge{
int to,w,net;
Edge(){}
Edge(int to,int w,int net)
{
this.to=to;
this.w=w;
this.net=net;
}
}
public class Main{
static final int MAXN=1005;
static final int INF=0x3f3f3f3f;
static int m,n;
static int[] head = new int[MAXN];
static Edge[] es = new Edge[4005];
static int tot;
static void addedge(int u,int v,int w)
{
es[tot] = new Edge(v,w,head[u]);
head[u] = tot++;
} static int[] d = new int[MAXN];
static boolean[] vis = new boolean[MAXN];
static int spfa(int src,int ter)
{
Arrays.fill(vis, false);
Arrays.fill(d, INF);
Queue<Integer> que = new LinkedList<Integer>();
que.add(src);
d[src]=0;
while(!que.isEmpty())
{
int u=que.peek();que.poll();
vis[u]=false;
for(int i=head[u];i!=-1;i=es[i].net)
{
Edge e = es[i];
if(d[e.to]>d[u]+e.w)
{
d[e.to]=d[u]+e.w;
if(!vis[e.to])
{
que.add(e.to);
vis[e.to]=true;
}
}
}
}
return d[ter];
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext())
{
tot=0;
Arrays.fill(head, -1);
m=in.nextInt();
n=in.nextInt();
for(int i=0;i<m;i++)
{
int u,v,w;
u=in.nextInt();
v=in.nextInt();
w=in.nextInt();
addedge(u,v,w);
addedge(v,u,w);
}
int res=spfa(n,1);
System.out.println(res);
}
}
}