zz's Mysterious Present
Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
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.
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
解题思路:仔细分析就是最短路的水题,只不过要反向建边,最后排序即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 305;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
struct Node
{
int place,speed,dist,id;
double time;
}p[maxn];
int n,m,k,map[maxn][maxn],dis[maxn];
bool vis[maxn];
void Dijkstra(int s)
{
int u,MIN;
memset(vis,false,sizeof(vis));
for(int i = 1; i <= n; i++)
if(i != s)
dis[i] = map[s][i];
dis[s] = 0;
vis[s] = true;
for(int i = 1; i < n; i++)
{
MIN = inf,u = -1;
for(int j = 1; j <= n; j++)
{
if(vis[j] == true) continue;
if(MIN > dis[j])
{
MIN = dis[j];
u = j;
}
}
if(u == -1) break;
vis[u] = true;
for(int j = 1; j <= n; j++)
{
if(vis[j] == false && dis[j] > dis[u] + map[u][j])
dis[j] = dis[u] + map[u][j];
}
}
}
bool cmp(Node a,Node b)
{
if(fabs(a.time - b.time) > eps)
return a.time < b.time;
else if(a.dist != b.dist)
return a.dist > b.dist;
else return a.id > b.id;
}
int main()
{
int u,v,w,des;
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
memset(map,inf,sizeof(map));
for(int i = 1; i <= n; i++) map[i][i] = 0;
for(int i = 1; i <= k; i++)
{
scanf("%d%d%d",&u,&v,&w);
map[v][u] = min(map[v][u],w);
}
scanf("%d",&des);
Dijkstra(des);
for(int i = 1; i <= m; i++)
{
scanf("%d",&p[i].place);
p[i].id = i;
p[i].dist = dis[p[i].place];
}
for(int i = 1; i <= m; i++)
{
scanf("%d",&p[i].speed);
if(dis[p[i].place] == inf)
p[i].time = inf;
else p[i].time = p[i].dist * 1.0 / p[i].speed;
}
sort(p+1,p+1+m,cmp);
if(p[1].dist == inf)
printf("No one\n");
else printf("%d\n",p[1].id);
}
return 0;
}