最短路径问题
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 19581 Accepted Submission(s): 5827
Problem Description
给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。
Input
输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点。n和m为0时输入结束。
(1<n<=1000, 0<m<100000, s != t)
(1<n<=1000, 0<m<100000, s != t)
Output
输出 一行有两个数, 最短距离及其花费。
Sample Input
3 2 1 2 5 6 2 3 4 5 1 3 0 0
Sample Output
9 11
Source
此题比起一般最短路径问题多了一个花费,其他的都没什么,同样不能使用Floyd算法,那这里小编给出SPFA,和Dijkstra两种算法的代码。
SPFA:
#include <iostream>//SPFA
#include <cstdio>
#include <cstdlib>
#include <queue>
#define MAX 0xfffffff
using namespace std;
struct Node
{
int p,d;
int th;
bool operator>(const Node &node)const
{
if(d == node.d)
return p>node.p;
return d>node.d;
}
};
bool visit[1001];
int mapd[1001][1001],mapp[1001][1001],dis[1001],ps[1001];
priority_queue<Node, vector<Node>, greater<Node> > q;
int main()
{
int n,m;
int a,b,d,p;
while(scanf("%d%d",&n,&m)&&n!=0&&m!=0)
{
for(int i=1; i<=n; i++)
{
visit[i] = false;
dis[i] = MAX;
ps[i] = MAX;
for(int j=1;j<=n;j++)
{
if(i == j)
{
mapd[i][j] = mapp[i][j] = 0;
continue;
}
mapd[i][j] = MAX;
mapp[i][j] = MAX;
}
}
for(int i=0; i<m; i++)
{
scanf("%d%d%d%d",&a,&b,&d,&p);
if(d<mapd[a][b])
{
mapd[a][b] = mapd[b][a] = d;
mapp[a][b] = mapp[b][a] = p;
}
if(d == mapd[a][b] && p < mapp[a][b])
{
mapp[a][b] = mapp[b][a] = p;
}
}
int s,t;
scanf("%d%d",&s,&t);
Node node;
node.th = s;
node.d = 0;
node.p = 0;
q.push(node);
while(!q.empty())
{
Node node = q.top();
q.pop();
if(visit[node.th]) continue;
visit[node.th] = true;
for(int i=1; i<=n; i++)
{
if(i!=node.th && mapd[node.th][i] != MAX && visit[i] == false)
{
if(node.d + mapd[node.th][i] < dis[i])
{
dis[i] = node.d + mapd[node.th][i];
ps[i] = node.p + mapp[node.th][i];
Node tmp;
tmp.th = i;
tmp.d = dis[i];
tmp.p = ps[i];
q.push(tmp);
}
else if(node.d + mapd[node.th][i] == dis[i] && node.p + mapp[node.th][i] < ps[i])
{
ps[i] = node.p + mapp[node.th][i];
Node tmp;
tmp.th = i;
tmp.d = dis[i];
tmp.p = ps[i];
q.push(tmp);
}
}
}
}
if(s == t)
printf("%d %d\n",0,0);
else printf("%d %d\n",dis[t],ps[t]);
}
return 0;
}
Dijkstra:
#include <stdio.h>//Dijkstra算法
#include <iostream>
#include <cstring>
#define INF 0xfffffff
#define N 1000+10
using namespace std;
int map[N][N],cost[N][N];
int min_dis,min_cost;
void Dijkstra(int s,int t,int n)
{
int dis[N],cost1[N];
bool vis[N];
memset(vis,false,sizeof(vis));
for(int i=1; i<N; i++)
{
dis[i] = map[s][i];
cost1[i] = cost[s][i];
}
dis[s] = 0;
vis[s] = true;
for(int i=1; i<n; i++)
{
int min = INF;
int k;
for(int j=1; j<=n; j++)
{
if(!vis[j] && min>dis[j])
{
min = dis[j];
k = j;
}
}
vis[k] = true;
for(int j=1; j<=n; j++)
{
if(!vis[j] && dis[j]>min+map[k][j])
{
dis[j] = min+map[k][j];
cost1[j] = cost1[k]+cost[k][j];
}
else if(!vis[j] && dis[j] == min + map[k][j] && cost1[j] > cost[k][j] + cost1[k])
{
cost1[j] = cost[k][j] + cost1[k];
}
}
if(vis[t])
{
min_dis = dis[t];
min_cost = cost1[t];
return;
}
}
min_dis = dis[t];
min_cost = cost1[t];
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)&&(n||m))
{
for(int i=1; i<=n; i++)
for(int j=1; j<i; j++)
{
map[i][j] = map[j][i] = INF;
cost[i][j] = cost[j][i] = INF;
}
while(m--)
{
int a,b,d,p;
scanf("%d%d%d%d",&a,&b,&d,&p);
if(map[a][b] > d)
{
map[a][b] = map[b][a] = d;
cost[a][b] = cost[b][a] = p;
}
}
int s,t;
scanf("%d%d",&s,&t);
Dijkstra(s,t,n);
printf("%d %d\n",min_dis,min_cost);
}
return 0;
}
在这里大家可以比较一下两种方法的代码量,相同点以及区别,在最短路径中加上Floyd算法,这三种算法都要务必掌握。