天天看點

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