hdu 2145 反向建边最短路

zz's Mysterious Present

Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 721    Accepted Submission(s): 154


Problem Description
There are m people in n cities, and they all want to attend the party which hold by zz. They set out at the same time, and they all will choose the best way they think, but due to someone take a ride, someone drive, and someone take a taxi, they have different speed. Can you find out who will get zz's mysterious present? The first one get the party will get the present . If there are several people get at the same time, the one who stay in the city which is farther from the city where is zz at begin will get the present. If there are several people get at the same time and the distance from the city he is at begin to the city where zz is, the one who has the larger number will get the present.
 

Input
The first line: three integers n, m and k. m is the total number of the people, and n is the total number of cities, and k is the number of the way.(0<n<=300, 0<m<=n, 0<k<5000)
The second line to the (k+1)th line: three integers a, b and c. There is a way from a to b, and the length of the way is c.(0<a,b<=n, 0<c<=100)
The (k+2)th line: one integer p(0<p<=n), p is the city where zz is.
The (k+3)th line: m integers. the ith people is at the place p[i] at begin.(0<p[i]<=n)
The (k+4)th line: m integers. the speed of the ith people is speed[i];(0<speed[i]<=100) 
All the ways are directed.
 

Output
For each case, output the one who get the present in one line. If no one can get the present, output "No one".
 

Sample Input
3 1 3 1 2 2 1 3 3 2 3 1 3 2 1
 

Sample Output
1

 

题意:有n个城市,m个人,zz住在某个城市要开会,m个人要从自己所在的城市去zz所在的城市,第一个到达的将会获得一个特殊的礼物,假设有多个人同时到达,就取距离最远的,否则取编号最大的。

解题思路:反向建边,求一次最短路,然后结构体排序。

代码:

 

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<map>
#include<list>
#include<set>
#include<queue>
#include<stack>
using namespace std;
const int inf=1000000000;
const int maxn=3010;
int dist[maxn],head[maxn],tol,m,n,id,pre[maxn];
struct Edge
{
        int st,to,next,val;
}edge[100*maxn];
void add(int u,int v,int w)
{
        edge[tol].st=u;
        edge[tol].to=v;
        edge[tol].next=head[u];
        edge[tol].val=w;
        head[u]=tol++;
}
struct Node
{
        int id,dist;
        Node(int a=0,int b=0):id(a),dist(b){}
        bool operator < (const Node &b) const
        {
                return dist>b.dist;
        }
};
struct PP
{
        int id,dist,speed,pos;
}pp[50*maxn],ss[50*maxn];
void fun(int st)
{
        int i,j,u,v;
        priority_queue<Node> q;
        q.push(Node(st,0));
        while(!q.empty())
        {
                Node ret=q.top();q.pop();
                u=ret.id;
                if(dist[u]<ret.dist)continue;
                int flag=1;
                for(i=head[u];i!=-1;i=edge[i].next)
                {
                        v=edge[i].to;
                        if(dist[v]>dist[u]+edge[i].val)
                        {
                                pre[v]=u;
                                dist[v]=dist[u]+edge[i].val;
                                q.push(Node(v,dist[v]));
                        }
                }
        }
}
bool cmp(PP a,PP b)
{
       if(a.dist*b.speed!=a.speed*b.dist)return a.dist*b.speed<a.speed*b.dist;
       if(a.dist!=b.dist)return a.dist>b.dist;
       return a.id>b.id;
}
int main()
{
        int i,j,k,a,b,c,pos;
        while(~scanf("%d%d%d",&n,&m,&k))
        {
                 memset(head,-1,sizeof(head));tol=0;
                 while(k--)
                 {
                       scanf("%d%d%d",&a,&b,&c);
                       add(b,a,c);
                 }
                 scanf("%d",&pos);
                 for(i=0;i<m;i++)scanf("%d",&pp[i].pos);
                 for(i=0;i<m;i++)scanf("%d",&pp[i].speed);
                 for(i=0;i<m;i++)pp[i].id=i+1;
                 for(i=0;i<=n;i++)dist[i]=inf;dist[pos]=0;
                 fun(pos);
                 for(i=0;i<m;i++)pp[i].dist=dist[pp[i].pos];
                 int cnt=0;
                 for(i=0;i<m;i++)if(pp[i].dist<inf)ss[cnt++]=pp[i];
                // for(i=1;i<=n;i++)cout<<dist[i]<<" ";cout<<endl;
                
// for(i=0;i<m;i++)cout<<pp[i].dist<<" ";cout<<endl;
                 if(cnt==0)
                 {
                         puts("No one");continue;
                 }
                 if(cnt==1)
                 {
                         printf("%d\n",ss[0].id);continue;
                 }
                 sort(ss,ss+cnt,cmp);
                 printf("%d\n",ss[0].id);
        }
        return 0;
}

 

posted @ 2013-09-16 17:01  线性无关  阅读(131)  评论(0编辑  收藏  举报