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;
    }
    

      



  • 相关阅读:
    cron表达式详解(转载)
    Swagger 3.0使用教程(转载)
    springboot整合shiro-对密码进行MD5并加盐处理(十五)(转载)
    redis排序
    引用和指针的区别?
    测试策略
    主键、外键的作用,索引的优点与不足?
    您所熟悉的软件测试类型都有哪些?请试着分别比较这些不同的测试类型的区别与联系(如功能测试、性能测试……)
    UI测试测什么
    数据库,数据库管理系统,数据库系统三者的区别和练习?
  • 原文地址:https://www.cnblogs.com/grglym/p/7694088.html
Copyright © 2011-2022 走看看