2015
04-13

# 最短路径问题

(1<n<=1000, 0<m<100000, s != t)

(1<n<=1000, 0<m<100000, s != t)

3 2
1 2 5 6
2 3 4 5
1 3
0 0

9 11

HDU-3790-最短路径问题

http://acm.hdu.edu.cn/showproblem.php?pid=3790

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
#define INF 0x7fffffff
int n,m;
int map[1005][1005];
int cost[1005][1005];
void dijkstra(int st,int ed)
{
int i,j,v,Min;
int visit[1005],dis[1005],value[1005];
for(i=1;i<=n;i++)
{
dis[i]=map[st][i];
value[i]=cost[st][i];
}
memset(visit,0,sizeof(visit));
visit[st]=1;
for(i=1;i<n;i++)
{
Min=INF;
for(j=1;j<=n;j++)
if(!visit[j]&&dis[j]<Min)
{
v=j;
Min=dis[j];
}
visit[v]=1;
for(j=1;j<=n;j++)
{
if(!visit[j]&&map[v][j]<INF)
{
if(dis[j]>dis[v]+map[v][j])
{
dis[j]=dis[v]+map[v][j];
value[j]=value[v]+cost[v][j];
}
else if(dis[j]==dis[v]+map[v][j])
{
if(value[j]>value[v]+cost[v][j])
value[j]=value[v]+cost[v][j];
}
}
}
}
printf("%d %d\n",dis[ed],value[ed]);
}
int main()
{
int i,j,st,ed;
int a,b,c,d;
while(scanf("%d%d",&n,&m),n||m)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
map[i][j]=INF;
cost[i][j]=INF;
}
while(m--)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
if(map[a][b]>c)
{
map[a][b]=map[b][a]=c;
cost[a][b]=cost[b][a]=d;
}
else if(map[a][b]==c)
{
if(cost[a][b]>d)
cost[a][b]=cost[b][a]=d;
}
}
scanf("%d%d",&st,&ed);
dijkstra(st,ed);
}
return 0;
}


1. I like your publish. It is great to see you verbalize from the coronary heart and clarity on this essential subject matter can be easily noticed.