1030 Travel Plan

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.朴素

#include<iostream>
#include<vector>
#include<queue>
const int maxn =505;
const int INF =0x3f3f3f3f;
using namespace std;
int G[maxn][maxn],d[maxn],cost[maxn][maxn],c[maxn];
int N,M,S,D;
int pre[maxn];
bool vis[maxn]={false};
void dijkstra(int s){
	fill(d,d+maxn,INF);
	fill(c,c+maxn,INF);
	for(int i=0;i<N;i++) pre[i] = i;
	d[s]=0;
	c[s]=0;
	for(int i=0;i<N;i++){
		int u=-1,min=INF;
		for(int j=0;j<N;j++){
			if(!vis[j] && d[j] < min){
				u=j;
				min=d[j];
			}
		}
		if(u == -1) return;
		vis[u] = true;
		for(int i=0;i<N;i++){
			if(!vis[i] && G[u][i] < INF){
				if(d[u] + G[u][i] < d[i]){
					d[i] = d[u] +G[u][i];
					c[i] =c[u] + cost[u][i]; 
					pre[i] = u;
				}
				else if(d[u] + G[u][i] == d[i] && c[i] > c[u] + cost[u][i]){
					c[i] = c[u] + cost[u][i];
					pre[i] = u ;
				}
			}
		}
	}
}
void print(int v){
	if(v == S){
		cout<<v<<' ';
		return;
	}
	print(pre[v]);
	cout<<v<<' ';
}
int main(){
	cin>>N>>M>>S>>D;
	fill(G[0],G[0] + maxn*maxn,INF);
	for(int i=0;i<M;i++){
		int x,y,z,w;
		cin>>x>>y>>z>>w;
		G[x][y] = z;
		G[y][x] = z;
		cost[x][y] = w;
		cost[y][x] =w;
	}
	dijkstra(S);
	print(D); 
	cout<<d[D]<<' '<<c[D];
	return 0;
} 

2.堆优化

#include<iostream>
#include<vector>
#include<queue>
const int maxn =505;
const int INF =0x3f3f3f3f;
using namespace std;
struct edge{
	int to,dis,money;
	edge(int to,int dis,int money) :to(to),dis(dis),money(money){};
};
struct Node{
	int to,dis;
	Node(int to,int dis) : to(to),dis(dis){};
	bool operator < (const Node other)const{
		return dis>other.dis;
	} 
};
int d[maxn],c[maxn];
vector<edge> graph[maxn];
int N,M,S,D;
int pre[maxn];
bool vis[maxn]={false};
void print(int v){
	if(v == S){
		cout<<v<<' ';
		return;
	}
	print(pre[v]);
	cout<<v<<' ';
}
int main(){
	cin>>N>>M>>S>>D;
	fill(d,d+maxn,INF);
	fill(c,c+maxn,INF);
	for(int i=0;i<N;i++) pre[i] = i;
	for(int i=0;i<M;i++){
		int x,y,z,w;
		cin>>x>>y>>z>>w;
		graph[x].push_back(edge(y,z,w));
		graph[y].push_back(edge(x,z,w));
	}
	priority_queue<Node> q;
	q.push(Node(S,0));
	c[S]=0;
	d[S]=0;
	while(!q.empty()){
		int u =q.top().to;
		q.pop();
		if(vis[u]) continue;
		if(u == D) break;
		vis[u] ==true;
		for(int i=0;i<graph[u].size();i++){
			int v=graph[u][i].to;
			int money=graph[u][i].money;
			int dist=graph[u][i].dis;
			if(d[u] + dist < d[v]){
				d[v] = d[u] + dist ;
				c[v] = c[u] + money;
				pre[v] = u; 
				q.push(Node(v,d[v]));
			}
			else if(d[u] + dist == d[v] && c[v] > c[u] + money){
				c[v] = c[u] + money ;
				pre[v] = u;
			}
		}
	}
	print(D); 
	cout<<d[D]<<' '<<c[D];
	return 0;
} 

还有一个dijkstra +dfs的算法,我感觉不如上面2个简单点,感觉就是在算路径的时候在算最小值
算法笔记P389

===================================================================
算了还是补上吧

#include<iostream>
#include<vector>
#include<queue>
const int maxn =505;
const int INF =0x3f3f3f3f;
using namespace std;
int G[maxn][maxn],d[maxn],cost[maxn][maxn];
int mincost=INF;
int N,M,S,D;
vector<int> pre[maxn];
vector<int> path,tempath;
bool vis[maxn]={false};
void dijkstra(int s){
	fill(d,d+maxn,INF);
	d[s]=0;
	for(int i=0;i<N;i++){
		int u=-1,min=INF;
		for(int j=0;j<N;j++){
			if(!vis[j] && d[j] < min){
				u=j;
				min=d[j];
			}
		}
		if(u == -1) return;
		vis[u] = true;
		for(int i=0;i<N;i++){
			if(!vis[i] && G[u][i] < INF){
				if(d[u] + G[u][i] < d[i]){
					d[i] = d[u] +G[u][i];
					pre[i].clear();
					pre[i].push_back(u);
				}
				else if(d[u] + G[u][i] == d[i] ){
					pre[i].push_back(u) ;
				}
			}
		}
	}
}
void DFS(int v){
	if(v == S){
	   tempath.push_back(v);
	   int tempcost =0;
	   for(int i=tempath.size()-1;i>0;i--){
	   	  int id=tempath[i],nextid=tempath[i-1];
	   	  tempcost+=cost[id][nextid];
	   }
	   if(tempcost < mincost){
	   	 mincost = tempcost;
	   	 path = tempath;
	   }
	   tempath.pop_back();//删除刚加入的节点
	   return;	
	} 
	tempath.push_back(v);
	for(int i=0;i<pre[v].size();i++){
		DFS(pre[v][i]);
	}
	tempath.pop_back();//遍历完所有前驱节点,删除该节点,遍历另外一个分支,有的话
}
int main(){
	cin>>N>>M>>S>>D;
	fill(G[0],G[0] + maxn*maxn,INF);
	for(int i=0;i<M;i++){
		int x,y,z,w;
		cin>>x>>y>>z>>w;
		G[x][y] = z;
		G[y][x] = z;
		cost[x][y] = w;
		cost[y][x] =w;
	}
	dijkstra(S);
	DFS(D);
	for(int i=path.size()-1;i>=0;i--){
		cout<<path[i]<<' ';
	}
	cout<<d[D]<<' '<<mincost;
	return 0;
} 

从思路上就是先算出最短路径下的路径,然后再单独算第二权值
1边权之和

int value = 0;
for(int i=tempath.size()-1;i>0;i--){
	int id=tempath[i],nextid=tempath[i-1];
	value+=cost[id][nextid];
}

2.点权之和

for(int i=tempath.size()-1;i>0;i--){
	int id=tempath[i];
	value+=weight[i];
}

3.最短路径数
开一个全局变量num
当你计算第二权值的时候,如果到达了叶子(起点)时,num++;

===================================================================
堆优化
算法笔记P387

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值