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;
}
#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;
}
相关文章推荐
- POJ1511 Invitation Cards [最短路,dijstra+heap,spfa]
- 1.1.1最短路(Floyd、Dijstra、BellmanFord)
- 最短路——Dijstra
- POJ 3653 & ZOJ 2935 & HDU 2722 Here We Go(relians) Again(最短路dijstra)
- HDU 1385 Minimum Transport Cost (Dijstra 最短路)
- POJ 3653 & ZOJ 2935 & HDU 2722 Here We Go(relians) Again(最短路dijstra)
- hdu3790 最短路径问题<最短路>
- 最短路 (Dijstra) Til the Cows Come Home
- uva 658 It's not a Bug, it's a Feature! 最短路dijstra
- 单源最短路——Dijstra
- hdu3790 最短路径问题 (dijkstra,双关键值最短路)
- POJ 2253 - 最短路变形 SPFA+Dijstra
- HDOJ 2066 一个人的旅行(Dijstra 类似多源最短路)
- poj 2449(A*+dijstra求k短路)
- HDU - 6166 SPFA最短路次短路 or 二进制+Dijstra
- POJ 2253 Dijstra 最短路变形
- 最短路(Floyd、Dijstra,BellmanFord)
- 最短路dijstra 堆优化
- hdu3790 (最短路)
- hdu2544 最短路 Dijstra算法堆优化,Bellman-Ford,Bellman-Ford队列优化