Meeting HDU - 5521
原题链接
考察:最短路
思路:
建边是容易想到的...但是本蒟蒻没反应过来怎么求会面点...
建虚点,以1,n为起点求到达每个点的距离,然后每个点距离的最大值的最小值就是答案.
Code
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
typedef long long LL;
typedef pair<LL,int> PII;
const int N = 100010,M = 1000010;
int n,m,h[N<<1],idx,ans[N];
bool st[N<<1];
LL dist[2][N<<1];
struct Road{
int fr,to,ne,w;
}road[M<<1];
void add(int a,int b,int c)
{
road[idx].w = c,road[idx].fr = a,road[idx].to = b,road[idx].ne = h[a],h[a] = idx++;
}
void dijkstra(int s,LL dist[])
{
for(int i=0;i<n<<1;i++) dist[i] =1e14;
memset(st,0,sizeof st);
priority_queue<PII,vector<PII>,greater<PII> > q;
dist[s] = 0;
q.push({0,s});
while(q.size())
{
PII it = q.top();
q.pop();
int u = it.second;
if(st[u]) continue;
st[u] = 1;
for(int i=h[u];~i;i=road[i].ne)
{
int v =road[i].to;
if(dist[v]>dist[u]+road[i].w)
{
dist[v] = dist[u]+road[i].w;
q.push({dist[v],v});
}
}
}
}
int main()
{
int T,kcase = 0;//我是菜狗QAQ
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
idx = 0;
memset(h,-1,sizeof h);
for(int i=1;i<=m;i++)
{
int t,s,x; scanf("%d%d",&t,&s);
while(s--)
{
scanf("%d",&x);
add(n+i,x,t),add(x,n+i,0);
}
}
dijkstra(1,dist[0]);
dijkstra(n,dist[1]);
LL res = 1e14;
for(int i=1;i<=n;i++)
if(max(dist[0][i],dist[1][i])<res)
res = max(dist[0][i],dist[1][i]);
if(res==1e14)
{
printf("Case #%d: Evil John\n",++kcase);
continue;
}
printf("Case #%d: %lld\n",++kcase,res);
int cnt = 0;
for(int i=1;i<=n;i++)
if(max(dist[0][i],dist[1][i])==res)
ans[++cnt] = i;
for(int i=1;i<=cnt;i++)
printf("%d%c",ans[i],i==cnt?'\n':' ');
}
return 0;
}