Meeting (完全图建图+最短路)

网友投稿 245 2022-08-30

Meeting (完全图建图+最短路)

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 n blocks labelled from 1 to n. Bessie lives in the first block while Elsie lives in the n-th one. They have a map of the farm which shows that it takes they ti minutes to travel from a block in Ei to another block in Ei where 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), the number of test cases. Then T test cases follow. The first line of input contains n and m. 2 ≤ n ≤ 105 . The following m lines describe the sets Ei (1 ≤ i ≤ m). Each line will contain two integers ti(1 ≤ ti ≤ 109 ) and Si (Si > 0) firstly. Then Si integer follows which are the labels of blocks in Ei . It is guaranteed that ∑m i=1 Si ≤ 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. 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. 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

题目大概:

给你n个点,m个团,每个团的点都是完全图,每个团的边权一样,都是w。然后两个人从1 ,n 出发,到达各个点的最少时间是多少,最少时间的点全部输出,如果不通,输出Evil John。

思路:

主要是建图的时候,可以把一个团的外面抽象一个虚点,然后把每个点的边权都连向虚点,这样,团内的点的距离问题,和两个团重合的问题也都解决了。但是权值加了一倍,最后需要  / 2.

当建完图就是最短路的模板题了。

代码:

#includeusing namespace std;#define ll long longconst int mod=1e9+7;const int INF=0x3f3f3f3f;const int MAXN=2000010;struct qnode{ int v; int c; qnode(int _v=0,int _c=0):v(_v),c(_c) {} bool operator <(const qnode &r)const { return c>r.c; }};struct Edge{ int v,cost; Edge(int _v=0,int _cost=0):v(_v),cost(_cost) {}};vectorE[MAXN];bool vis[MAXN];int dist[MAXN];//点的编号从 1 开始void Dijkstra(int n,int start){ memset(vis,false,sizeof(vis)); for(int i=1; i<=n; i++)dist[i]=INF; priority_queueque; while(!que.empty())que.pop(); dist[start]=0; que.push(qnode(start,0)); qnode tmp; while(!que.empty()) { tmp=que.top(); que.pop(); int u=tmp.v; if(vis[u])continue; vis[u]=true; for(int i=0; idist[u]+cost) { dist[v]=dist[u]+cost; que.push(qnode(v,dist[v])); } } }}void addedge(int u,int v,int w){ E[u].push_back(Edge(v,w));}int a[MAXN],b[MAXN],c[MAXN];int main(){ int t; int ans=1; scanf("%d",&t); while(t--) { int n,m; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); scanf("%d%d",&n,&m); for(int i=1;i<=n+m;i++)E[i].clear(); for(int i=1; i<=m; i++) { int w,m1; scanf("%d%d",&w,&m1); for(int j=1; j<=m1; j++) { int u; scanf("%d",&u); addedge(u,i+n,w); addedge(i+n,u,w); } } Dijkstra(n+m,1); for(int i=1;i<=n;i++) { a[i]=dist[i]; } Dijkstra(n+m,n); for(int i=1;i<=n;i++) { b[i]=dist[i]; } int min_1=INF; for(int i=1;i<=n;i++) { c[i]=max(a[i],b[i]); min_1=min(min_1,c[i]); } if(min_1==INF) { printf("Case #%d: Evil John\n",ans++); continue; } printf("Case #%d: %d\n",ans++,min_1/2); int flag=0; for(int i=1;i<=n;i++) { if(c[i]==min_1) { printf("%s%d",flag==1?" ":"",i); flag=1; } } puts(""); } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:YTU 2920: Shape系列-7
下一篇:不能让“双十一”营销套路成为“连续剧”!(双十一的营销手段和效果)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~