zoukankan      html  css  js  c++  java
  • hdu 2680:Choose the best route(Dijkstra , SPFA)

    http://acm.hdu.edu.cn/showproblem.php?pid=2680

    Problem Description

    One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n.

    Input

    There are several test cases.
    Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home.
    Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes .
    Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations.

    Output

    The output contains one line for each data set : the least time Kiki needs to spend ,if it’s impossible to find such a route ,just output “-1”.

    Sample Input

    5 8 5
    1 2 2
    1 5 3
    1 3 4
    2 4 7
    2 5 6
    2 3 5
    3 5 1
    4 5 1
    2
    2 3
    4 3 4
    1 2 3
    1 3 4
    2 3 2
    1
    1

    Sample Output

    1
    -1

    题意分析:

    有n个车站,可以选取w个中任意一个作为起点,车站之间有单向通行时间,求到达终点的最短时间。

    解题思路:

    因为有多个起点,把终点当作起点,起点当终点, 反向建边,求出最短路后比较所有终点的值。

    注意有重复边。

    Dijkstra算法:

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #define N 1020
    using namespace std;
    int n, m, s, w, inf=0x3f3f3f3f;
    int a[N], e[N][N], dis[N];
    bool book[N];
    void init()
    {
    	int i, j, u, v, c;
    	for(i=1; i<=n; i++)
    		for(j=1; j<=n; j++)
    			if(i==j)
    				e[i][j]=0;
    			else e[i][j]=inf;
    	for(i=1; i<=m; i++){
    		scanf("%d%d%d", &u, &v, &c);
    		if(e[v][u]>c)
    			e[v][u]=c;
    	}
    	scanf("%d", &w);
    	for(i=1; i<=w; i++)
    		scanf("%d", &a[i]);		
    }
    int Dijkstra()
    {
    	int mini, u;
    	memset(book, false, sizeof(book));
    	book[s]=true;
    	for(int i=1; i<=n; i++)
    		dis[i]=e[s][i];
    	for(int i=1; i<n; i++)
    	{
    		mini=inf;
    		for(int j=1; j<=n; j++){
    			if(book[j]==false && dis[j]<=mini){
    				mini=dis[j];
    				u=j;
    			}	
    		}	
    		book[u]=true;
    		for (int v=1; v<=n; v++)
    			if (book[v]==false && dis[v]>dis[u]+e[u][v])
    				dis[v]=dis[u]+e[u][v]; 
    	} 
    	int ans=inf;
    	for(int i=1; i<=w; i++)
    		ans=min(ans, dis[a[i]]);
    	if(ans==inf)
    		return -1;
    	return ans;
    }
    int main()
    {
    	while(scanf("%d%d%d", &n, &m, &s)!=EOF)
    	{
    		init();
    		printf("%d
    ", Dijkstra());
    	}
    	return 0;
    } 

    SPFA:

    #include <stdio.h>
    #include <string.h>
    #include <vector>
    #include <queue>
    #include <algorithm>
    #define N 1020
    using namespace std;
    int n, m, s, w, inf=0x3f3f3f;
    int a[N], dis[N];
    bool book[N];
    struct edge{
    	int v;
    	int w;
    	edge(int v, int w) : v(v), w(w) {}
    };
    vector <edge>e[N];
    void init()
    {
    	int i, j, u, v, c;
    	for(i=1; i<=n; i++)
    		e[i].clear();
    	for (i=1; i<=n; i++)
    		dis[i]=inf;
    	dis[s]=0; 
    	for (i=1; i<=m; i++){
    		scanf("%d%d%d", &u, &v, &c);
    		e[v].push_back(edge(u, c));
    	}
    	scanf("%d", &w);
    	for (i=1; i<=w; i++)
    		scanf("%d", &a[i]);		
    }
    int SPFA()
    {
    	int mini, u;
    	queue<int>q;
    	memset(book, false, sizeof(book));
    	q.push(s);
    	book[s]=true;
    	while(!q.empty())
    	{
    		u=q.front();
    		q.pop();
    		book[u]=false;
    		for(int i=0; i<e[u].size(); i++){
    			if(dis[e[u][i].v]>dis[u]+e[u][i].w)
    			{
    				dis[e[u][i].v]=dis[u]+e[u][i].w;
    				if(book[e[u][i].v] == false)
    				{
    					q.push(e[u][i].v);
    					book[e[u][i].v]=true;
    				} 
    			}
    		}
    	}
    	int ans=inf;
    	for(int i=1; i<=w; i++)
    		ans=min(ans, dis[a[i]]);
    	if(ans==inf)
    		return -1;
    	return ans;
    }
    int main()
    {
    	while(scanf("%d%d%d", &n, &m, &s)!=EOF)
    	{
    		init();
    		printf("%d
    ", SPFA());
    	}
    	return 0;
    } 
  • 相关阅读:
    程序编译与代码优化 -- 早期(编译期)优化
    Java字节码指令
    知识点
    Openresty配置文件上传下载
    Openresty + nginx-upload-module支持文件上传
    G1日志分析
    Garbage First(G1)垃圾收集器
    Java内存分析工具jmap
    编译JDK1.7
    Java服务CPU占用高问题定位方法
  • 原文地址:https://www.cnblogs.com/zyq1758043090/p/11852511.html
Copyright © 2011-2022 走看看