soj 12647. Sightseeing

12647. Sightseeing

Constraints

Time Limit: 20 secs, Memory Limit: 256 MB

Description

A travel agent wants to organize a sightseeing trip in a city for tourists. The city can be modelled as a connected graph, where each node represents a tourist site, and each edge represents a two-way road. Unfortunately, not all roads are good; some roads may be bad due to traffic jam. The agent does not want to disappoint the tourists by visiting the bad roads, and hence wants to compute the best path to take. He assigns a quality value to each road in such a way that a better road has a higher quality value. He also defines the quality value of a path to be the minimum quality among all roads along the path. The following figure depicts an example of 4 tourist sites and 4 roads modelled as a graph.

Each edge in the graph is associated with a value which represents the quality of the road. For e.g. the edge (1, 2), which represents the road connecting node 1 and 2, has quality 10; while the edge (3, 4) has quality 5. The quality of path 1-2-4 is the minimum among the two edges (1, 2) and (2, 4) and thus is min{10, 20} = 10. Likewise, the quality of path 1-2-4-3 is the minimum among the three edges (1, 2), (2, 4) and (3, 4), which is min{10, 20, 5} = 5. Node 1 is the hotel where the tourists stay. Given a destination X, the travel agent wants to find the highest quality among all possible paths from node 1 to node X.

For instance, suppose they want to visit node 4. In the example above, among all possible paths from node 1 to node 4, the highest quality is achieved by the path 1-2-4, with quality 10. On the other hand, if they want to visit node 3 instead of node 4, the highest quality achievable is 30.

Furthermore, the travel agent is not satisfied in knowing the highest quality for a single destination. He has a list of destinations in mind, and he wants to know the highest quality for each of them. Specifically, when given a list of Q sites X1,X2, . . . ,XQ, he wants to know the highest quality from node 1 to node X1, the high quality from node 1 to node X2, and so on.

Input

The first line in the input contains 3 integers, V , E and Q (V<=500,000, E<=5,000,000, Q<=V-1), which represent the number of tourist sites, the number of edges and the number of destinations respectively. The tourist sites are labelled from 1 to V where node 1 denotes the hotel where the tourists start their trip. Next, it is followed by E lines where each line contains 3 integers, v1, v2, and q, where v1 and v2 denote the sites that the road connects and q denotes the quality of the road (0<=q<=100,000). Next, it is followed by Q lines where each line contains an integer X which represents a destination in the travel agent’s list, and X <> 1 (i.e. the hotel is not in the list).

Output

Your program must write to standard output for each destination an integer which is the highest quality achievable.

Sample Input

4 4 2
1 2 10
1 3 30
2 4 20
3 4 5
3
4

Sample Output

30
10

Problem Source

2014年每周一赛第十三场

题意:求1到x的路径,最小的最大

思路:类似最短路,直接spfa,不过spfa会卡,改加了优化的dij就好了

// Problem#: 12647
// Submission#: 3287907
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <queue>
#define LL long long
#define maxn 500010
#define MAX 5000011
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;

struct node
{
    int u,dis;
    node(){}
    node(int x,int y):u(x),dis(y){}
    bool operator<(const node &s) const{
        return dis < s.dis;
    }
};
int que[maxn],dis[maxn];
int head[maxn],to[MAX*2],val[MAX*2];
int next1[MAX*2] ,top ;
bool vi[maxn] ;
void Unit(int &u,int &v,int &c)
{
    next1[top]=head[u];to[top]=v;
    val[top]=c;head[u]=top++;
}
void spfa(int s)
{
    memset(dis,-1,sizeof(dis)) ;
    memset(vi,0,sizeof(vi)) ;
    dis[s]=INF ;
    int i,v;
    priority_queue<node>q;
    q.push(node(s,dis[s])) ;
    node aa;
    while(!q.empty())
    {
        aa=q.top();q.pop();
        if(vi[aa.u]) continue;
        vi[aa.u]=true;
        for( i = head[aa.u] ; i != -1 ; i = next1[i])
        {
            v=to[i] ;
            if(dis[v]<min(aa.dis,val[i]))
            {
                dis[v]=min(aa.dis,val[i]) ;
                q.push(node(v,dis[v]));
            }
        }
    }
}
int main()
{
    int i,n,m,j,Max;
    int len ,k,q ;
    int u,v,c;
    while(scanf("%d%d%d",&n,&m,&q ) != EOF)
    {
         top=0;
         memset(head,-1,sizeof(head)) ;
         while(m--)
         {
             scanf("%d%d%d",&u,&v,&c) ;
             Unit(u,v,c) ;
             Unit(v,u,c) ;
         }
         spfa(1) ;
         while(q--)
         {
             scanf("%d",&u) ;
             printf("%d\n",dis[u]) ;
         }
    }
    return 0 ;

}                                 

时间: 2024-10-08 16:32:48

soj 12647. Sightseeing的相关文章

Soj题目分类

-----------------------------最优化问题------------------------------------- ----------------------常规动态规划  SOJ1162 I-Keyboard  SOJ1685 Chopsticks SOJ1679 Gangsters SOJ2096 Maximum Submatrix  SOJ2111 littleken bg SOJ2142 Cow Exhibition  SOJ2505 The County

poj1734 Sightseeing trip

Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6919   Accepted: 2646   Special Judge Description There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attra

POJ 1637 Sightseeing tour (混合图欧拉回路)

Sightseeing tour Description The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can see every corner of the beautiful city. They want to construct the tour so that every street in the city is visit

1999 Central European Olympiad in Informatics - Sightseeing Trip

算法提示 最小环问题 题目大意 在一张带权无向图上,找出至少含 3 个点且权值和最小的环,并按环上的循序输出环上的点.存在重边,无自环. 做法分析 参考最小环问题,在更新 dist[i][j] 时,记录更新其的点 k,便于回溯路径. 参考代码 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <queue> 5 #include <algorithm> 6

SOJ 4445 2015四川省赛模拟题

背景:赛场上就是因为没开这道题,而没拿到银,回来A了,感觉代码能力还是很弱,一定要先想好再敲,而且注重代码的函数化,这样无论是观感,还是调试都要好很多,逻辑要清晰,看代码要仔细,提交之前通读代码. 题意:起点在原点的frog,开始向右运动,且碰到障碍物就右转,问转多少次? 思路:关键是图的大小范围是109,无法存下,只有用类似链表的方法来存图.这里用了两个容器,一个以X为基准,一个一Y为基准,这两个容器设置很特殊,是为了满足题中特殊的查询需要:查询前进方向最近障碍物. 我的代码: #includ

HDU1688 Sightseeing(SPFA 求最短路与次短路的路径条数)可用作模板

Sightseeing Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 718    Accepted Submission(s): 293 Problem Description Tour operator Your Personal Holiday organises guided bus trips across the Bene

poj3621 Sightseeing Cows --- 01分数规划

典型的求最优比例环问题 参考资料: http://blog.csdn.net/hhaile/article/details/8883652 此题中,给出每个点和每条边的权值,求一个环使 ans=∑点权/∑边权 最大. 因为题目要求一个环,而且必然是首尾相接的一个我们理解的纯粹的环,不可能是其他样子的环, 所以我们可以把一条边和指向的点看做整体处理. 上面方程可以化为:ans×e[i]-p[i]=0 以它为边权二分答案,spfa求负环,有负环则该ans可行,增大下界. 若一直不可行,则无解. #i

POJ 1734:Sightseeing trip

Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6831 Accepted: 2612 Special Judge Description There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attractions

URAL 1004 Sightseeing Trip(最小环)

Sightseeing Trip Time limit: 0.5 secondMemory limit: 64 MB There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attractions, sightseeing the town. To earn as much as possible from this a