觉得浮夸了四年,漠然发现原来是浮躁了四年!

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 }
复制代码

 

 
 
posted @   heat nan  阅读(194)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示