M - Meeting HDU - 5521
Bessie and her friend Elsie decide to have a meeting. However, after Farmer John decorated his
fences they were separated into different blocks. John's farm are divided into nnblocks labelled from 11 to nn.
Bessie lives in the first block while Elsie lives in the nn-th one. They have a map of the farm
which shows that it takes they titi minutes to travel from a block in EiEi to another block
in EiEi where Ei (1≤i≤m)Ei (1≤i≤m) is a set of blocks. They want to know how soon they can meet each other
and which block should be chosen to have the meeting.
Input
The first line contains an integer T (1≤T≤6)T (1≤T≤6), the number of test cases. Then TTtest cases
follow.
The first line of input contains nn and mm. 2≤n≤1052≤n≤105. The following mm lines describe the sets Ei (1≤i≤m)Ei (1≤i≤m). Each line will contain two integers ti(1≤ti≤109)ti(1≤ti≤109)and Si (Si>0)Si (Si>0) firstly. Then SiSi integer follows which are the labels of blocks in EiEi. It is guaranteed that ∑mi=1Si≤106∑i=1mSi≤106.
Output
For each test case, if they cannot have the meeting, then output "Evil John" (without quotes) in one line.
Otherwise, output two lines. The first line contains an integer, the time it takes for they to meet.
The second line contains the numbers of blocks where they meet. If there are multiple
optional blocks, output all of them in ascending order.
Sample Input
2 5 4 1 3 1 2 3 2 2 3 4 10 2 1 5 3 3 3 4 5 3 1 1 2 1 2
Sample Output
Case #1: 3 3 4 Case #2: Evil John
Hint
In the first case, it will take Bessie 1 minute travelling to the 3rd block, and it will take Elsie 3 minutes travelling to the 3rd block. It will take Bessie 3 minutes travelling to the 4th block, and it will take Elsie 3 minutes travelling to the 4th block. In the second case, it is impossible for them to meet. 给你n个点,m个街区,保证所有的街区中的城市小于1e6。因为街区中的城市两两到达应该是给出的街区的时间。所以很难直接建边,所以我们每个街区建立一个虚点,这个虚点是街区中的点过来花费t,虚点回去不花钱。这样跑一次1-n的最短路,再跑一次n-1的最短路即可。
bug:用 long long ;
inf赋值可能会改变
vector记得清空,清完(包括虚点)
//邻接表+优先队列优化的dijstla模板
#include<bits/stdc++.h>
using namespace std;
const int M=1000000000;
struct HeadNode{
int d,u;
bool operator < (const HeadNode& rhs) const{
return d>rhs.d;
}
};
struct edge{
int to;
int cost;
};
vector<edge>G[10005];
bool vis[10005];
int n,m,x,s,t;
int d[10005];
void Dij(){
fill(d+1,d+n+1,M);
d[s]=0;
priority_queue<HeadNode>Q;
Q.push((HeadNode){0,s});
while(!Q.empty()){
HeadNode x=Q.top();Q.pop();
int u=x.u;
if(vis[u])continue;
vis[u]=1;
for(int i=0;i<G[u].size();i++){
edge e=G[u][i];
if(d[e.to]>d[u]+e.cost){
d[e.to]=d[u]+e.cost;
Q.push((HeadNode){d[e.to],e.to});
}
}
}
}
int main(){
cin>>n>>m>>s>>t;
for(int i=1;i<=m;i++){
edge e;
scanf("%d%d%d",&x,&e.to,&e.cost);
G[x].push_back(e);
}
Dij();
if(d[t]==M)printf("-1");
else printf("%d",d[t]);
return 0;
}
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll inf=0x3f3f3f3f;
struct node
{
ll d;
int u;
};
bool operator<(const node &a,const node &b)
{
return a.d>b.d;
}
priority_queue <node> q;
struct edge
{
int to;
ll cost;
};
vector <edge> G[400005];
bool vis[400005];
ll dis1[400005],dis2[400005];
void dijstla1(int s)
{
memset(vis,0,sizeof(vis));
memset(dis1,inf,sizeof(dis1));
dis1[s]=0;
q.push((node){0,s});
while(!q.empty())
{
node x=q.top();
q.pop();
int u=x.u;
if(vis[u]) continue;
vis[u]=1;
for(int i=0;i<G[u].size();i++)
{
edge e=G[u][i];
if(dis1[e.to]>dis1[u]+e.cost)
{
dis1[e.to]=dis1[u]+e.cost;
q.push((node){dis1[e.to],e.to});
}
}
}
}
void dijstla2(int s)
{
memset(vis,0,sizeof(vis));
memset(dis2,inf,sizeof(dis2));
dis2[s]=0;
q.push((node){0,s});
while(!q.empty())
{
node x=q.top();
q.pop();
int u=x.u;
if(vis[u]) continue;
vis[u]=1;
for(int i=0;i<G[u].size();i++)
{
edge e=G[u][i];
if(dis2[e.to]>dis2[u]+e.cost)
{
dis2[e.to]=dis2[u]+e.cost;
q.push((node){dis2[e.to],e.to});
}
}
}
}
int main()
{
int T,cas=0;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
ll t;
int num;
scanf("%lld%d",&t,&num);
while(num--)
{
int x;
scanf("%d",&x);
G[i+n].push_back((edge){x,0}); //建个虚点
G[x].push_back((edge){i+n,t});
}
}
dijstla1(1);
dijstla2(n);
ll ans=1e18;
for(int i=1;i<=n;i++)
{
ans=min(ans,max(dis1[i],dis2[i]));
}
if(dis1[n]!=4557430888798830399) printf("Case #%d: %lld\n",++cas,ans);
else {printf("Case #%d: Evil John\n",++cas);continue;}
int flag=0;
for(int i=1;i<=n;i++)
{
if(max(dis1[i],dis2[i])==ans)
{
if(!flag) {printf("%d",i);flag=1;}
else printf(" %d",i);
}
}
printf("\n");
for(int i=0;i<400002;i++) G[i].clear();
}
return 0;
}