URAL 1085 Meeting(最短路)

时间:2023-11-13 21:54:26

Meeting

Time limit: 2.0 second
Memory limit: 64 MB
K
friends has decided to meet in order to celebrate their victory at the
programming contest. Unfortunately, because of the tickets rise in price
there is a problem: all of them live in different parts of the city,
and they are to choose a place of meeting so that they wouldn't pay too
much for the tickets. You are to help them make the best choice.
All stops are enumerated with integers 1, …, N inclusive. There are M
tram routes in the city (the friends take only trams and do not go on
foot from stop to stop). For each route numbers of its stops are known.
For each friend we know an amount of money he has and whether he has a
month tram ticket. A ticket price equals 4 rubles.
You
are to find out a stop number, such that all of the friends might come
there and the sum of money they spend for their tramps would be minimal.
Naturally, they may change routes (it means that each one may make
changes on his way to the required stop). Note, that changing the route
one has to pay for a new ticket: the friends are honest people — they do
always pay for tickets. Everyone pays for a ticket from his own money.
No one is to leave money for the return tickets.

Input

The first line contains two integers N and M; 1 ≤ N, M ≤ 100 (N is a number of stops, M is a number of routes). The next M lines define the routes in the following sort: there is an integer L in the beginning of a line — that is an amount of stops of the corresponding route (2 ≤ L ≤ 100). Then L
integers defining stops numbers of the route follow. The numbers are
separated with a space. A route is defined by its stops along the line
in one direction.

The next line contains an integer K (1 ≤ K ≤ 100), that is an amount of friends. The next K
lines contain information about each of them (one line for one person):
there is a positive integer in the beginning of a line that is an
amount of money (in rubles) the person has, then a number of a stop that
he goes there from his home on foot, then 0 (if this person has no
month ticket) or 1 (if he has). The numbers in a line are separated with
a space. No one of the friends has more than 1000 rubles.

Output

Output
a number of a stop that is a meeting
point (if there are several numbers choose the minimal one) and a total
sum of money (in rubles) that the friends has paid for their trips to
the appointed place. The numbers should be separated with a space. If
the friends won't be able to meet at one stop, output the only number 0.

Sample

input output
4 3
2 1 2
2 2 3
2 3 4
3
27 1 0
15 4 0
45 4 0
4 12
Problem Author: Alexander Somov
【题意】一个城市中有很多公交站,每条公交路线链接一些公交站(双向),每辆公交车车票为4卢布。有那么一群朋友住在其中的一些车站,他们想选择一个公交站聚会,使得每个人都能靠自己的钱到达聚会地点,且总花费最短。现给定所有人的钱,出发地点,是否具有月票(有月票的不需要花钱),求聚会地点(若有多个满足条件,输出编号最小的)和总花费。
【分析】考虑到一条公交路线上的点只需要花一张车票,所以将一条公交路线上的点的距离都赋值1,然后Floyd求出任意两点最短路,然后枚举答案。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 1e3+;
const int M = +;
const int mod=1e9+;
int n=,m,k,tot=,t;
int head[N],vis[N],in[N],sum[N];
int s[N],dis[N][N],mo[N];
vector<int>vec,vv[N];
void Floyd(){
for(int k=;k<=n;k++){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(dis[i][k]!=inf&&dis[k][j]!=inf&&dis[i][j]>dis[i][k]+dis[k][j]){
dis[i][j]=dis[i][k]+dis[k][j];
}
}
}
}return;
}
int main()
{
met(dis,inf);
for(int i=;i<N;i++)dis[i][i]=;
int u,v;
scanf("%d%d",&n,&m);
while(m--){
scanf("%d",&k);
while(k--){
scanf("%d",&u);
vec.pb(u);
}
for(int i=;i<vec.size();i++){
for(int j=i+;j<vec.size();j++){
dis[vec[i]][vec[j]]=dis[vec[j]][vec[i]]=;
}
}vec.clear();
}
Floyd();
scanf("%d",&k);
for(int i=;i<=k;i++){
scanf("%d%d%d",&mo[i],&s[i],&vis[i]);
mo[i]/=;
}
int ans=inf;
for(int i=;i<=n;i++){
int sum=;
for(int j=;j<=k;j++){
if((!vis[j]&&mo[j]<dis[i][s[j]])||dis[i][s[j]]==inf){sum=inf;break;}
if(!vis[j]&&mo[j]>=dis[i][s[j]])sum+=dis[i][s[j]];
}
if(sum==inf)continue;
if(sum<ans){
ans=sum;vv[ans].pb(i);
}
else if(sum==ans&&ans!=inf)vv[ans].pb(i);
}
if(ans!=inf){sort(vv[ans].begin(),vv[ans].end()); printf("%d %d\n",vv[ans][],ans*);}
else printf("0\n");
return ;
}