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
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
#define maxn 1008
int W[maxn][maxn];
int Wc[maxn][maxn];
int dis[maxn];
int cost[maxn];
inline int min(int a,int b)
{
return a>b?b:a;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2 &&(n||m))
{
memset(W,0x3f,sizeof(W));
memset(dis,0x3f,sizeof(dis));
memset(cost,0x3f,sizeof(cost));
memset(Wc,0x3f,sizeof(Wc));
for(int i=1;i<=m;i++)
{
int a,b,d,p;
scanf("%d%d%d%d",&a,&b,&d,&p);
if(W[a][b] > d)
{
W[a][b] = W[b][a] = d;
Wc[a][b] = Wc[b][a] = p;
}
else if(W[a][b] == d)
Wc[a][b] = Wc[b][a] = min(Wc[a][b],p);
}
queue <int> q;
int s,t;
scanf("%d%d",&s,&t);
dis[s] = cost[s] = 0;
q.push(s);
while(!q.empty())///等下如果超时就用邻接表
{
int p = q.front();
q.pop();
for(int i=1;i<=n;i++)
{
if(dis[p] + W[p][i] < dis[i] || (dis[p] + W[p][i] == dis[i] && cost[p] + Wc[p][i] < cost[i]))
{
dis[i] = dis[p] + W[p][i];
cost[i] = cost[p] + Wc[p][i];
q.push(i);
}
}
}
printf("%d %d\n",dis[t],cost[t]);
}
return 0;
}
下面是用邻接表的SPFA。。显然会快一些。187MS。上面的一半还少
#include <iostream>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 1008
int dis[maxn];
int key[maxn];
struct Edge
{
int v,dis,cost;
}edge;
vector <Edge> ans[maxn];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2 &&(n||m))
{
memset(dis,0x3f,sizeof(dis));
memset(key,0x3f,sizeof(key));
for(int i=1;i<=n;i++)
{
ans[i].clear();
}
for(int i=1;i<=m;i++)
{
int u,v,d,p;
scanf("%d%d%d%d",&u,&v,&p,&d);
edge.v = v;
edge.dis = p;
edge.cost = d;
ans[u].push_back(edge);
edge.v = u;
ans[v].push_back(edge);
}
int s,t;
scanf("%d%d",&s,&t);
dis[s] = 0;
key[s] = 0;
queue <int> q;
q.push(s);
while(!q.empty())
{
int p = q.front();
q.pop();
for(int i=0;i<ans[p].size();i++)
{
if(dis[p] + ans[p][i].dis < dis[ans[p][i].v] ||(dis[p] + ans[p][i].dis == dis[ans[p][i].v] && key[p] + ans[p][i].cost < key[ans[p][i].v]))
{
dis[ans[p][i].v] = dis[p] + ans[p][i].dis;
key[ans[p][i].v] = key[p] + ans[p][i].cost;
q.push(ans[p][i].v);
}
}
}
printf("%d %d\n",dis[t],key[t]);
}
return 0;
}
接下来是优先队列优化的Dijkstra算法i:注意和Prim算法还是有点不同的,Prim是每次找终点为标记的最短的边。而优先队列优化的Dijkstra算法
每次是要找到不在森林中的距离最短的点。所以赶脚Edge需要起点和终点
/*
接下来用Dijkstra算法
优先队列优化一下帅一点
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
#define maxn 1008
bool vis[maxn];
int Dis[maxn];
int Cost[maxn];
int n,m,s,t;
struct Edge
{
int ss,v,dis,cost;
bool operator < (const Edge & a) const
{
if(Dis[ss]+dis > Dis[a.ss]+a.dis) return 1;
if(Dis[ss]+dis < Dis[a.ss]+a.dis) return 0;
if(Dis[ss]+dis == Dis[a.ss]+a.dis)
{
return Cost[ss]+cost > Cost[a.ss]+a.cost;
}
}
}edge;
vector <Edge> ans[maxn];
void Dijkstra()
{
priority_queue <Edge> q;
edge.cost = 0;
edge.dis = 0;
edge.ss = s;
edge.v = s;
q.push(edge);
while(!vis[t] && !q.empty())
{
L:
edge = q.top();
q.pop();
while(vis[edge.v] && !q.empty()) goto L;
if(!vis[edge.v])
{
int nowv = edge.v;
vis[edge.v] = 1;
for(int i=0;i<ans[nowv].size();i++)
{
if(!vis[ans[nowv][i].v])
{
if(Dis[nowv] + ans[nowv][i].dis < Dis[ans[nowv][i].v] ||(Dis[nowv] + ans[nowv][i].dis == Dis[ans[nowv][i].v]
&& Cost[nowv] + ans[nowv][i].cost < Cost[ans[nowv][i].v] ))
{
Dis[ans[nowv][i].v] = Dis[nowv] + ans[nowv][i].dis;
Cost[ans[nowv][i].v] = Cost[nowv] + ans[nowv][i].cost;
edge.ss = nowv;
edge.v = ans[nowv][i].v;
edge.dis = ans[nowv][i].dis;
edge.cost = ans[nowv][i].cost;
q.push(edge);
}
}
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)==2 &&(n||m))
{
for(int i=1;i<=n;i++) ans[i].clear();
memset(vis,0,sizeof(vis));
memset(Dis,0x3f,sizeof(Dis));
memset(Cost,0x3f,sizeof(Cost));
for(int i=1;i<=m;i++)
{
int u,v,d,p;
scanf("%d%d%d%d",&u,&v,&d,&p);
edge.dis = d;
edge.cost = p;
edge.ss = u;
edge.v = v;
ans[u].push_back(edge);
edge.v = u;
edge.ss = v;
ans[v].push_back(edge);
}
scanf("%d%d",&s,&t);
Dis[s] = Cost[s] = 0;
Dijkstra();
printf("%d %d\n",Dis[t],Cost[t]);
}
return 0;
}