2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学) Meeting(SPAF+拆点)
Meeting
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3618 Accepted Submission(s): 1165
Problem Description
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.
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 ∑mi=1Si≤106.
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 ∑mi=1Si≤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.
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.
Source
123
【思路】
两遍SPFA 一个从1 一个从n 开始
问题在于 数据边太大, 一般暴力 肯定会超时, 因此 在 建边上优化, 对于每个集合 建立 一个 出口点, 每次扫 这个出口点, n个集合 就是n个点
这样大大优化建边
【注意】
可能会爆 int 用long long
【代码】
#include <iostream> #include <stdio.h> #include <algorithm> #include <cmath> #include <math.h> #include <cstring> #include <string> #include <queue> #include <stack> #include <stdlib.h> #include <list> #include <map> #include <set> #include <bitset> #include <vector> #define mem(a,b) memset(a,b,sizeof(a)) #define findx(x) lower_bound(b+1,b+1+bn,x)-b #define FIN freopen("input.txt","r",stdin) #define FOUT freopen("output.txt", What a Ridiculous Election"w",stdout) #define S1(n) scanf("%d",&n) #define SL1(n) scanf("%I64d",&n) #define S2(n,m) scanf("%d%d",&n,&m) #define SL2(n,m) scanf("%I64d%I64d",&n,&m) #define Pr(n) printf("%d\n",n) #define lson rt << 1, l, mid #define rson rt << 1|1, mid + 1, r using namespace std; typedef long long ll; const ll INF=1e18+10; const int maxn=2e6+5; const int MOD=1000000007; const int mod=1e9+7; int dir[5][2]={0,1,0,-1,1,0,-1,0}; int n,m; ll dis[maxn],dis1[maxn]; int head[maxn]; int ans[maxn],tol; ll cont; int vis[maxn]; struct Edge{ int v,w,next; }edge[maxn]; void init() { cont=0; memset(head,-1,sizeof(head)); } void add(int u,int v,int w) { edge[++cont].v=v; edge[cont].w=w; edge[cont].next=head[u]; head[u]=cont; } void SPFA(int st) { queue<int> Q; for(int i=0;i<=n+m+3;i++) dis[i]=INF; mem(vis,0); dis[st]=0; Q.push(st); vis[st]=1; while(!Q.empty()) { ll u=Q.front(); Q.pop(); vis[u]=0; for(int t=head[u];t!=-1;t=edge[t].next) { ll v=edge[t].v; ll w=edge[t].w; if(dis[v]>dis[u]+w) { dis[v]=dis[u]+w; if(!vis[v]) { vis[v]=1; Q.push(v); } } } } } int main() { int T,cot=0; cin>>T; int te,num,x; while(T--) { scanf("%d %d",&n,&m); init(); for(int i=1;i<=m;i++) { scanf("%d %d",&te,&num); for(int j=1;j<=num;j++) { scanf("%d",&x); add(x,i+n,te); add(i+n,x,te); } } SPFA(1); printf("Case #%d: ",++cot); if(dis[n]==INF) { printf("Evil John\n"); continue; } for(int i=0;i<=maxn;i++) dis1[i]=dis[i]; SPFA(n); tol=0; ll res=INF; for(int i=1;i<=n;i++) { res=min(res,max(dis[i],dis1[i])); } printf("%lld\n",res/2); tol=1; for(int i=1;i<=n;i++) { if(max(dis[i],dis1[i])==res) ans[tol++]=i; } for(int i=1;i<tol;i++) { if(i==1) printf("%d",ans[i]); else printf(" %d",ans[i]); } printf("\n"); } return 0; }
岂曰无衣?与子同袍。王于兴师,修我戈矛。与子同仇!
岂曰无衣?与子同泽。王于兴师,修我矛戟。与子偕作!
岂曰无衣?与子同裳。王于兴师,修我甲兵。与子偕行!