hdu 2145(zz's Mysterious Present) (最短路+排序)

zz's Mysterious Present

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


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
 
这一题整整半天是有了!关键还是题目没有看透!
题目明明说是 All the ways are directed.
而且还是红色字体,那么的明显为什么不看呢!贡献了n个WA后,google一下,才发现是有向图。
倒是大家都说的反向见图,还是有这个意识的,刚开始就这样搞的。
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 #define N 305
 7 #define MAX 0xfffffff
 8 struct node{
 9     int root;//出发地点
10     int speed;//速度
11     double time;//时间
12     int min_len;//最短距离
13     int num;//第i个人的编号
14 }s[N];
15 int f[N];
16 int mark[N];
17 int map[N][N];
18 int n,m,k;
19 int p;
20 bool cmp(node a,node b)
21 {
22     if(a.time!=b.time)
23      return a.time<b.time;
24     else if(a.min_len!=b.min_len)
25         return a.min_len>b.min_len;
26     else 
27         return a.num>b.num;
28 }
29 void Dijkstra()
30 {
31     int i,j,kk;
32     memset(mark,0,sizeof(mark));
33     for(i=1;i<=n;i++)
34     f[i]=map[p][i];
35     f[p]=0;
36     for(i=1;i<=n;i++)
37     {
38         int min=MAX;
39         for(j=1;j<=n;j++)
40         {
41             if(!mark[j]&&f[j]<min)
42             {
43                 min=f[j];
44                 kk=j;
45             }
46         }
47         if(min==MAX)  break;
48         mark[kk]=1;
49         for(j=1;j<=n;j++)
50         {
51             if(f[j]>f[kk]+map[kk][j])
52                 f[j]=f[kk]+map[kk][j];
53         }
54     }
55 
56 }
57 int main()
58 {
59         while(scanf("%d%d%d",&n,&m,&k)!=EOF)
60         {
61             memset(s,0,sizeof(s));
62             int i,j;
63               for(i=1;i<=n;i++)
64                   for(j=1;j<=n;j++)
65                       map[i][j]=MAX;
66           
67               for(i=1;i<=k;i++)
68               {
69                   int a,b,c;
70                   scanf("%d%d%d",&a,&b,&c);
71                   map[b][a]=c;
72               }
73               scanf("%d",&p);
74               for(j=0;j<m;j++)
75               {
76                   scanf("%d",&s[j].root);
77                   s[j].num=j+1;
78           
79               }
80               for(j=0;j<m;j++)
81                   scanf("%d",&s[j].speed);
82               Dijkstra();
83                   
84               for(j=0;j<m;j++)
85               {
86                   
87                   s[j].min_len=f[s[j].root];
88                   s[j].time=1.0*s[j].min_len/s[j].speed;
89                  
90               }
91               sort(s,s+m,cmp);
92               if(s[0].min_len==MAX)
93                   printf("No one\n");
94               else
95                   printf("%d\n",s[0].num);
96         }
97         return 0;
98 }

 

 
 

你可能感兴趣的:(HDU)