zoukankan      html  css  js  c++  java
  • 1030. Travel Plan (30)

    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

    题目大意:求带权最短路,如果最短路有多种情况,则选择花费最少的情况。

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    #define max 502
    #define INF 0x6ffffff
    int map[max][max];
    vector<int>ShortPath;
    vector<int>CurPath;
    int curCost=0;
    int curPath=0;
    int minCost=INF;
    int minPath=INF;
    int n,m,s,d;
    int cost[max][max];
    int visit[max];
    void DFS(int sd){
    	if(minPath < curPath)return;
    	if(sd == d){
    		if(minPath>curPath){
    			minPath=curPath;
    			ShortPath=CurPath;
    			minCost=curCost;
    		}else if(minPath == curPath && minCost>curCost){
    			ShortPath=CurPath;
    			minCost=curCost;
    		}
    		return ;
    	}
    	int i;
    	for(i=0;i<n;i++){
    		if(visit[i]==0&&map[sd][i]!=INF){
    			int c=curCost;
    			int p=curPath;
    			curCost+=cost[sd][i];
    			curPath+=map[sd][i];
    			CurPath.push_back(i);
    			visit[i]=1;
    			DFS(i);
    			CurPath.pop_back();
    			curCost=c;
    			curPath=p;
    			visit[i]=0;
    		}
    	}
    }
    int main(){
    	memset(visit,0,sizeof(visit));
    	scanf("%d%d%d%d",&n,&m,&s,&d);
    	int i,j;
    	int c1,c2,d12,c12;
    	for(i=0;i<n;i++){
    		for(j=0;j<n;j++){
    			map[i][j]=INF;
    		}
    	}
    	for(i=0;i<m;i++){
    		scanf("%d%d%d%d",&c1,&c2,&d12,&c12);
    		map[c1][c2]=map[c2][c1]=d12;
    		cost[c1][c2]=cost[c2][c1]=c12;
    	}
    	visit[s]=1;
    	DFS(s);
    	printf("%d ",s);
    	int length=ShortPath.size();
    	for(i=0;i<length;i++){
    		printf("%d ",ShortPath[i]);
    	}
    	printf("%d %d
    ",minPath,minCost);
    	return 0;
    }
    

      



  • 相关阅读:
    20169221 2016-2017-2 《移动平台应用开发实践》第十一周学习总结
    20169221 2016-2017-2 《移动平台应用与开发》第十周实验总结
    20169201 2016-2017-2 《网络攻防实践》实验三 缓冲区溢出实验
    20169218 2016-2017-2 《网络攻防实践》第九周作业
    2016-2017 《移动平台开发》实验三 敏捷开发与XP实践
    20169221 2016-2017-2 实验一 网络攻防环境的搭建与测试
    实验二《Java面向对象》实验报告
    20169221 2016-2017-2 《移动平台开发》第七周学习总结
    20169221 2016-2017-2 《网络攻防》第七周学习总结
    20169221 2016-2017-2 《移动平台开发》第六周学习总结
  • 原文地址:https://www.cnblogs.com/grglym/p/7694088.html
Copyright © 2011-2022 走看看