• hdoj_1385Minimum Transport Cost


    Minimum Transport Cost

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 5392    Accepted Submission(s): 1354


    Problem Description
    These are N cities in Spring country. Between each pair of cities there may be one transportation track or none. Now there is some cargo that should be delivered from one city to another. The transportation fee consists of two parts: 
    The cost of the transportation on the path between these cities, and

    a certain tax which will be charged whenever any cargo passing through one city, except for the source and the destination cities.

    You must write a program to find the route which has the minimum cost.
     

    Input
    First is N, number of cities. N = 0 indicates the end of input.

    The data of path cost, city tax, source and destination cities are given in the input, which is of the form:

    a11 a12 ... a1N
    a21 a22 ... a2N
    ...............
    aN1 aN2 ... aNN
    b1 b2 ... bN

    c d
    e f
    ...
    g h

    where aij is the transport cost from city i to city j, aij = -1 indicates there is no direct path between city i and city j. bi represents the tax of passing through city i. And the cargo is to be delivered from city c to city d, city e to city f, ..., and g = h = -1. You must output the sequence of cities passed by and the total cost which is of the form:
     

    Output
    From c to d :
    Path: c-->c1-->......-->ck-->d
    Total cost : ......
    ......

    From e to f :
    Path: e-->e1-->..........-->ek-->f
    Total cost : ......

    Note: if there are more minimal paths, output the lexically smallest one. Print a blank line after each test case.

     

    Sample Input
    5 0 3 22 -1 4 3 0 5 -1 -1 22 5 0 9 20 -1 -1 9 0 4 4 -1 20 4 0 5 17 8 3 1 1 3 3 5 2 4 -1 -1 0
     

    Sample Output
    From 1 to 3 : Path: 1-->5-->4-->3 Total cost : 21 From 3 to 5 : Path: 3-->4-->5 Total cost : 16 From 2 to 4 : Path: 2-->1-->5-->4 Total cost : 17
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    #include<set>
    #include<string>
    #include<queue>
    #include <stack>
    using namespace std;
    #pragma warning(disable : 4996)
    
    const int MAXN = 2000;
    const int INF = 999999;
    int n, s, e;
    int maps[MAXN][MAXN];
    bool visited[MAXN];
    int pre[MAXN];
    int dist[MAXN];
    int values[MAXN];
    vector<int>path;
    
    int cmp(int a, int b, int c) 
    { 
    	int cur = 0, path1[1002] = {0}, path2[1002] = {0}, count1 = 0, count2 = 0; 
    	path1[count1++] = c, path2[count2++] = c;//path1与path2存的是到c点的路径 
    	for(cur = a; cur != pre[cur]; cur = pre[cur]) 
    		path1[count1++] = cur; 
    	for(cur = b; cur != pre[cur]; cur = pre[cur]) 
    		path2[count2++] = cur; 
    	count1--, count2--; 
    	while(count1 != -1 || count2 != -1) 
    	{ 
    		if(path1[count1] > path2[count2]) 
    			return b; 
    		else if(path1[count1] < path2[count2]) 
    			return a; 
    		count1--, count2--; 
    	} 
    	if(count1 == -1) 
    		return a; 
    	else 
    		return b; 
    }
    
    void Dijkstra() //起点,终点
    {
    	int i, j;
    	int minValue, minNode;
    
    	dist[s] = 0;
    	visited[s] = true;
    	for (i = 1; i <= n; i++)
    	{
    		dist[i] = maps[s][i];
    		if(dist[i] == -1)
    		{
    			pre[i] = 0;
    		}
    		else
    		{
    			pre[i] = s;
    			//printf("pre[%d] = %d\n", i, pre[i]);
    			dist[i] += values[i];
    		}
    		//printf("dist[%d] = %d\n", i, dist[i]);
    	}
    	for (i = 1; i <= n; i++)
    	{
    		minValue = INF;
    		minNode = 0;
    		for (j = 1; j <= n; j++)
    		{
    			if(!visited[j] && minValue > dist[j])
    			{
    				minNode = j;
    				minValue = dist[j];
    			}
    		}
    		//printf("j = %d dist[%d] = %d\n", minNode, minNode, minValue);
    		if(minNode == 0)
    		{
    			break;
    		}
    		visited[minNode] = true;
    		for (j = 1; j <= n; j++)
    		{
    			if(!visited[j] && maps[minNode][j] != INF && dist[j] > dist[minNode] + maps[minNode][j] + values[j])
    			{
    				dist[j] = dist[minNode] + maps[minNode][j] + values[j];
    				pre[j] = minNode;
    				//printf("pre[%d] = %d\n", j, pre[j]);
    			}
    			else if(!visited[j] && maps[minNode][j] != INF && dist[j] == dist[minNode] + maps[minNode][j] + values[j])
    			{
    				pre[j] = cmp(pre[j], minNode, j);
    			}
    		}
    	}
    }
    
    void show()
    {
    	int x = e;
    	while (pre[x] != x)
    	{
    		path.push_back(x);
    		x = pre[x];
    	}
    	printf("From %d to %d :\n", s, e);
    	printf("Path: ");
    
    	printf("%d-->", s);
    	for (int i = path.size() - 1; i >= 1; i--)
    	{
    		printf("%d-->", path[i]);
    	}
    	printf("%d\n", path[0]);
    	path.clear();
    
    	printf("Total cost : %d\n", dist[e] - values[e]);
    	printf("\n");
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int x;
    	while (scanf("%d", &n) != EOF)
    	{
    		if(n == 0)
    		{
    			break;
    		}
    		for (int i = 1; i <= n; i++)
    		{
    			for (int j = 1; j <= n; j++)
    			{
    				scanf("%d", &x);
    				if(x == -1 || x == 0)
    				{
    					maps[i][j] = INF;
    				}
    				else
    				{
    					maps[i][j] = x;
    				}
    			}
    		}
    		for (int i = 1; i <= n; i++)
    		{
    			scanf("%d", &values[i]);
    		}
    		while (scanf("%d %d", &s, &e) != EOF)
    		{
    			if(s == -1 && e == -1)
    			{
    				break;
    			}
    			if(s == e)
    			{
    				printf("From %d to %d :\n", s, e);
    				printf("Path: %d\n", s);
    				printf("Total cost : 0\n");
    				printf("\n");
    			}
    			else
    			{
    				memset(visited, false, sizeof(visited));
    				for (int i = 1; i <= n; i++)
    				{
    					pre[i] = i;
    				}
    				Dijkstra();
    				show();
    			}
    
    		}
    	}
    	return 0;
    }


  • 相关阅读:
    学习php中的正则表达式,PHP正则表达式基础
    在新浪云SAE中使用smarty引擎模版
    随笔
    HTML 基础(二)
    HTML笔记(一)
    数据挖掘之异常检测
    DHCP协议
    基础的Linux命令(二)
    基础的linux命令(一)
    VMware虚拟机安装
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835050.html
Copyright © 2020-2023  润新知