HDU_5521_Meeting
2018-06-17 21:48:08来源:未知 阅读 ()
Meeting
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3704 Accepted Submission(s): 1187
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.
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.
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.
- 在路径1->n中取一点使得max(time(1->vex),time(n->vex))最小
- 很容易考虑从1和n分别跑一遍单源最短路得到两个距离数组dis1和disn,然后逐个比较下就可以了
- 但是这题难点在于给出结点间关系是以set方式给予的,我们如果不能加速自当前点寻找可达点的查找速度就会tle
- 每个set看似分离,实际上因为set相交的缘故,在不同set中的两个点是可达的
- 我们可以先对于单个set考虑,可以通过缩点,对于一个set中的点用一个新的点表示,然后用新点和其他set相连
- 接下来就是考虑怎样建边使得同一个set中的点之间的距离相同
- 新加的点不会增加两个点之间的距离,自set中一点到新点不应有距离的增加,那么我们建边的时候set内的点到新点距离为0,新点到set内每一个点的距离都相同
- 通过这样的缩点和建边就可以将原本的set结构等价转换,我们只需要跑两遍最短路即可
1 #include <iostream> 2 #include <string> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <climits> 7 #include <cmath> 8 #include <vector> 9 #include <queue> 10 #include <stack> 11 #include <set> 12 #include <map> 13 using namespace std; 14 typedef long long LL ; 15 typedef unsigned long long ULL ; 16 const int maxn = 1e6 + 10 ; 17 const LL inf = 0x3f3f3f3f3f; 18 const int npos = -1 ; 19 const int mod = 1e9 + 7 ; 20 const int mxx = 100 + 5 ; 21 const double eps = 1e-6 ; 22 const double PI = acos(-1.0) ; 23 24 struct node{ 25 LL len; 26 int i; 27 bool operator < (const node &r)const{ 28 if(len!=r.len) 29 return len<r.len; 30 else 31 return i<r.i; 32 } 33 }; 34 struct enode{ 35 int v; 36 LL w; 37 int next; 38 }; 39 struct qnode{ 40 int u; 41 LL dis; 42 bool operator < (const qnode &r)const{ 43 return dis>r.dis; 44 } 45 }; 46 enode edge[maxn<<1]; 47 int head[maxn<<1], cnt; 48 void addedge(int x, int y, LL z){ 49 edge[cnt]=(enode){y,z,head[x]}; 50 head[x]=cnt++; 51 } 52 std::vector< node > buf; 53 int T, n, m, u, v, s, no1, non; 54 LL w, dis1[maxn], disn[maxn], ans; 55 bool vis[maxn]; 56 void DIJ(LL *dis, int source){ 57 LL now; 58 priority_queue< qnode > Q; 59 qnode temp; 60 for(int i=0;i<=n+m;i++){ 61 vis[i]=false; 62 dis[i]=inf; 63 } 64 dis[source]=0LL; 65 Q.push((qnode){source,dis[source]}); 66 while(!Q.empty()){ 67 temp=Q.top(); 68 Q.pop(); 69 if(!vis[temp.u]){ 70 u=temp.u; 71 now=temp.dis; 72 vis[u]=true; 73 for(int i=head[u];i!=-1;i=edge[i].next){ 74 v=edge[i].v; 75 w=edge[i].w; 76 if(now+w<=dis[v]){ 77 dis[v]=now+w; 78 Q.push((qnode){v,dis[v]}); 79 } 80 } 81 } 82 } 83 } 84 int main(){ 85 // freopen("in.txt","r",stdin); 86 // freopen("out.txt","w",stdout); 87 while(~scanf("%d",&T)){ 88 for(int kase=1;kase<=T;kase++){ 89 no1=0; 90 non=0; 91 scanf("%d %d",&n,&m); 92 cnt=0; 93 memset(head,-1,sizeof(head)); 94 for(int i=1;i<=m;i++){ 95 scanf("%lld %d",&w,&s); 96 for(int j=1;j<=s;j++){ 97 scanf("%d",&u); 98 if(u==1)no1=1; 99 if(u==n)non=1; 100 v=n+i; 101 addedge(u,v,0LL); 102 addedge(v,u,w); 103 } 104 } 105 printf("Case #%d: ",kase); 106 if(!(no1&&non)){ 107 printf("Evil John\n"); 108 continue; 109 } 110 DIJ(dis1,1); 111 DIJ(disn,n); 112 buf.clear(); 113 for(int i=1;i<=n;i++) 114 buf.push_back((node){max(dis1[i],disn[i]),i}); 115 sort(buf.begin(),buf.end()); 116 ans=buf[0].len; 117 if(ans==inf){ 118 printf("Evil John\n"); 119 }else{ 120 printf("%lld\n",ans); 121 printf("%d",buf[0].i); 122 int i=1; 123 while(i<buf.size() && buf[i].len==ans) 124 printf(" %d",buf[i++].i); 125 cout<<endl; 126 } 127 } 128 } 129 return 0; 130 }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- HDU-2955-Robberies(0-1背包) 2020-03-30
- hdu1455 拼木棍(经典dfs) 2020-02-29
- anniversary party_hdu1520 2020-02-16
- hdu1062 text reverse 2020-01-27
- hdu4841 2020-01-26
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash