您的位置:首页 > Web前端 > JavaScript

hdu3790(最短路dijstra)

2014-03-14 09:39 246 查看
题目链接:hdu3790

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;
const int N = 1006;
const int inf = 0xfffffff;

int map

,n;
int cost

;//花费
bool v
;
int dis
;
int val
;

void dijstra(int s, int e)
{
int i,j;
for(i = 1; i <= n; i ++)
{
dis[i] = map[s][i];
val[i] = cost[s][i];
v[i] = false;
}
v[s] = true;
for(i = 2; i <= n; i ++)
{
int minn = inf, pos = s;
for(j = 1; j <= n; j ++)
{
if(!v[j] && dis[j] < minn)
{
minn = dis[j];
pos = j;
}
}
v[pos] = true;
for(j = 1; j <= n; j ++)
{
if(!v[j] && map[pos][j] < inf)
{
if(dis[pos] + map[pos][j] < dis[j])
{
dis[j] = dis[pos] + map[pos][j];
val[j] = val[pos] + cost[pos][j];
}
if(dis[pos] + map[pos][j] == dis[j])//扩展到j点的距离相同时,判断最小花费
{
if(val[pos] + cost[pos][j] < val[j])
val[j] = val[pos] + cost[pos][j];
}
}
}
}
printf("%d %d\n",dis[e],val[e]);
}

int main()
{
int m,x,y,a,b,i,j;
while(scanf("%d%d",&n,&m),(n||m))
{
for(i = 1; i <= n; i ++)
for(j = 1; j <= n; j ++)
map[i][j] = cost[i][j] = inf;
while(m--)//注意判断重边
{
scanf("%d%d%d%d",&x,&y,&a,&b);
if(map[x][y] > a)
{
map[x][y] = map[y][x] = a;
cost[x][y] = cost[y][x] = b;
}
if(map[x][y] == a)
cost[x][y] = cost[y][x] = min(cost[x][y],b);
}
scanf("%d%d",&x,&y);
dijstra(x,y);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  最短路 dijstra