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

      



  • 相关阅读:
    DirectShow音频采集pcm,实时编码AAC,附源码
    live555 RTSP推送到Darwin出现404错误的解决
    live555 RTSP推送到Darwin出现404错误的解决
    公网RTSP地址(持续更新)
    公网RTSP地址(持续更新)
    手机Android音视频采集与直播推送,实现单兵、移动监控类应用
    手机Android音视频采集与直播推送,实现单兵、移动监控类应用
    用Darwin开发RTSP级联服务器(拉模式转发)(附源码)
    用Darwin开发RTSP级联服务器(拉模式转发)(附源码)
    TCP/IP和Socket的关系
  • 原文地址:https://www.cnblogs.com/grglym/p/7694088.html
Copyright © 2011-2022 走看看