A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:
City1 City2 Distance Cost
where the numbers are all integers no more than 500, and are separated by a space.
Output Specification:
For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.
Sample Input
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Sample Output
0 2 3 3 40
题意:找最短路径,如果最短路径不唯一时,要求花费更小,输出最短路径和最小权重以及最小花费。
思路:典型的最短路问题。
【1】 使用dijkstra算法,但是我的写法(邻接表)暂时没找到错误【第三组错误】,暂存。
【2】邻接矩阵, 由于n比较小,所以不用担心超时
【3】dfs, 参考:_Occult_
这道题不需要去重,有需要可以加上。
代码【邻接矩阵,24分】:
#include <iostream>
#include <sstream>
#include <string>
#include <cstdio>
#include <queue>
#include <cstring>
#include <map>
#include <stack>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define mem(a,n) memset(a,n,sizeof(a))
#define DBGS() cout<<"START\n"
#define DBGE() cout<<"END\n"
const int N = 5e3+5;
const ll INF = 0x3f3f3f3f;
const double eps=1e-4;
int n,m,s,t;
int vis[N];
struct Edge {
int to,w,p;
Edge(int x=INF,int y=INF):w(x),p(y) {}
bool operator < (const Edge &m)const {
if(w!=m.w)
return w>m.w;//先按照长度从小到大排序
return p>m.p;//长度相同按照花费排序
}
} e,ans;
vector<vector<Edge> >G;
int pre[N];
int mindist,mincost;
void dijkstra() {
memset(vis,0,sizeof(vis));
priority_queue<Edge>pq;
pq.push(e);
while(!pq.empty()) {
Edge now=pq.top();
pq.pop();
// printf("%d %d %d\n",now.to,now.w,now.p);
if(vis[now.to])
continue;
vis[now.to]=1;
if(now.to==t) {
mindist=now.w;
mincost=now.p;
return ;
}
for(int i=0; i<G[now.to].size(); i++) {
Edge next;
next.to=G[now.to][i].to;
if(vis[next.to])
continue;///已在队列中的不重复入队
if(next.w>G[now.to][i].w+now.w) {
next.w=G[now.to][i].w+now.w;
next.p=G[now.to][i].p+now.p;
pre[next.to]=now.to;
}
if(next.w==G[now.to][i].w+now.w&&next.p>G[now.to][i].p+now.p) {
next.p=G[now.to][i].p+now.p;
pre[next.to]=now.to;
}
pq.push(next);
}
}
}
int main() {
scanf("%d%d%d%d",&n,&m,&s,&t);
G.clear();
G.resize(n+1);
int a,b,w,p;
while(m--) {
scanf("%d%d%d%d",&a,&b,&w,&p);
e.to=b,e.w=w,e.p=p;
G[a].push_back(e);
e.to=a;
G[b].push_back(e);///无向边
}
e.to=s,e.w=0,e.p=0;
dijkstra();
int tmp=t;
vector<int>res;
while(tmp!=s) {
res.push_back(tmp);
tmp=pre[tmp];
}
res.push_back(s);
for(int i=res.size()-1; i>=0; i--) {
printf("%d ",res[i]);
}
printf("%d %d\n",mindist,mincost);
return 0;
}
【邻接矩阵,满分】:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define drep(i,n,a) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a,n,sizeof(a))
#define lowbit(i) ((i)&(-i))
typedef long long ll;
typedef unsigned long long ull;
const ll INF=0x3f3f3f3f;
const double eps = 1e-6;
const int N = 5e2+5;
int n,m,s,e;
int G[N][N];
int dis[N],cost[N][N];
int c[N],pre[N];
bool vis[N];
void dijkstra(int src) {
fill(dis,dis+N,INF);
fill(c,c+N,INF);
dis[src]=c[src]=0;
for(int i=0; i<n; i++) pre[i]=i;
for(int i=0; i<n; i++) {
int u=-1,minD=INF;
for(int j=0; j<n; j++) { ///找到一个未访问且距离最小的顶点
if(!vis[j]&&(u==-1||dis[j]<minD)) {
minD=dis[j];
u=j;
}
}
if(u==-1) return;///全部访问过
vis[u]=1;
for(int v=0; v<n; v++) { ///选择未访问过且u可达到的顶点
if(vis[v]||G[u][v]==INF) continue;
if(dis[u]+G[u][v]<dis[v]) {
dis[v]=dis[u]+G[u][v];
c[v]=c[u]+cost[u][v];
pre[v]=u;
} else if(dis[u]+G[u][v]==dis[v]&&c[u]+cost[u][v]<c[v]) {
c[v]=c[u]+cost[u][v];
pre[v]=u;
}
}
}
}
void dfs(int u) {
if(u==s) {
printf("%d ",u);
return ;
}
dfs(pre[u]);
printf("%d ",u);
}
int main() {
scanf("%d%d%d%d",&n,&m,&s,&e);
fill(G[0],G[0]+N*N,INF);
for(int i=0; i<m; i++) {
int u,v,dist,co;
scanf("%d%d%d%d",&u,&v,&dist,&co);
G[u][v]=G[v][u]=dist;
cost[u][v]=cost[v][u]=co;
}
dijkstra(s);
dfs(e);
printf("%d %d\n",dis[e],c[e]);
return 0;
}
【dfs,满分】:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define drep(i,n,a) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a,n,sizeof(a))
#define lowbit(i) ((i)&(-i))
typedef long long ll;
typedef unsigned long long ull;
const ll INF=0x3f3f3f3f;
const double eps = 1e-6;
const int N = 5e2+5;
int n,m,s,e;
int G[N][N];
int dis[N],cost[N][N];
int c[N],pre[N];
bool vis[N];
void dfs1(int u) {
if(u==e) return ;
for(int i=0; i<n; i++) {
if(G[u][i]==INF) continue;///不可达
if(dis[i]>dis[u]+G[u][i]) {
dis[i]=dis[u]+G[u][i];
c[i]=c[u]+cost[u][i];
pre[i]=u;
dfs1(i);
} else if(dis[i]==dis[u]+G[u][i]&&c[i]>c[u]+cost[u][i]) {
c[i]=c[u]+cost[u][i];
pre[i]=u;
dfs1(i);
}
}
}
//bool dfs2(int u) {
// if(u==s) {
// printf("%d ",u);
// return true;
// }
// for(int i=0; i<n; i++) {
// if(dis[u]==dis[i]+G[u][i]&&c[u]==c[i]+cost[u][i]) {
// if(dfs2(i)) {
// printf("%d ",u);
// return true;
// }
// }
// }
// return false;
//}
void dfs2(int u) {///从终点往前搜索
if(u==s) {
printf("%d ",u);
return ;
}
dfs2(pre[u]);
printf("%d ",u);
}
int main() {
scanf("%d%d%d%d",&n,&m,&s,&e);
fill(G[0],G[0]+N*N,INF);
for(int i=0; i<m; i++) {
int u,v,dist,co;
scanf("%d%d%d%d",&u,&v,&dist,&co);
G[u][v]=G[v][u]=dist;
cost[u][v]=cost[v][u]=co;
}
fill(dis,dis+N,INF);
fill(c,c+N,INF);
dis[s]=c[s]=0;
dfs1(s);
dfs2(e);
printf("%d %d\n",dis[e],c[e]);
return 0;
}