2017暑期集训Day 1

最短路与2-SAT

ref:http://blog.csdn.net/jarjingx/article/details/8521690

ref:http://www.cnblogs.com/shadowland/p/5872257.html

T1:洪水

Code:

 1 #include<iostream> 
 2 #include<cstdio> 
 3 #include<cstdlib> 
 4 #include<cstring> 
 5 #include<queue> 
 6 #include<algorithm> 
 7 #define mkp(x,y) make_pair(x,y) 
 8 #define inf 707406378 
 9 inline int in(){ 
10     int x=0;bool f=0; char c; 
11     for (;(c=getchar())<'0'||c>'9';f=c=='-'); 
12     for (x=c-'0';(c=getchar())>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0'); 
13     return f?-x:x; 
14 } 
15 using namespace std; 
16 int t[52][52],vis[52][52]; 
17 bool mp[52][52]; 
18 char ch[52]; 
19 int n,m,sx,sy,ex,ey; 
20 typedef pair<int,int> P; 
21 const int dx[4]={-1,0,1,0}; 
22 const int dy[4]={0,1,0,-1}; 
23 queue <P> q,q1; 
24 inline bool exbound(int x,int y){ 
25     return (x<0)||(x>=n)||(y<0)||(y>=m); 
26 } 
27 void bfs1(){ 
28     while (!q.empty()){ 
29         P p=q.front();q.pop(); 
30         for (int i=0;i<4;++i){ 
31             int curx=p.first+dx[i],cury=p.second+dy[i]; 
32             if (exbound(curx,cury)||mp[curx][cury]) continue; 
33             if ((curx==ex&&cury==ey)||t[curx][cury]!=inf) continue; 
34             t[curx][cury]=t[p.first][p.second]+1;q.push(mkp(curx,cury)); 
35         } 
36     } 
37 } 
38 void bfs2(){ 
39     memset(vis,127/3,sizeof(vis)); 
40     q1.push(mkp(sx,sy));vis[sx][sy]=0; 
41     while (!q1.empty()){ 
42         P p=q1.front();q1.pop(); 
43         for (int i=0;i<4;++i){ 
44             int curx=p.first+dx[i],cury=p.second+dy[i]; 
45             if (exbound(curx,cury)||mp[curx][cury]||vis[curx][cury]!=inf) continue; 
46             if (vis[p.first][p.second]+1>=t[curx][cury]) continue; 
47             vis[curx][cury]=vis[p.first][p.second]+1; 
48             if (curx==ex&&cury==ey) {printf("%d",vis[ex][ey]);return;} 
49             q1.push(mkp(curx,cury)); 
50         } 
51     }printf("-1"); 
52 } 
53 int main() 
54 { 
55     n=in();m=in();memset(t,127/3,sizeof(t)); 
56     memset(mp,0,sizeof(mp)); 
57     for (int i=0;i<n;++i){ 
58         scanf("%s",ch); 
59         for (int j=0;j<m;++j){ 
60             if (ch[j]=='D') ex=i,ey=j; 
61             if (ch[j]=='S') sx=i,sy=j; 
62             if (ch[j]=='X') mp[i][j]=1; 
63             if (ch[j]=='*') q.push(mkp(i,j)),t[i][j]=0; 
64         } 
65     }bfs1();bfs2(); 
66     return 0; 
67 }

T2:Toll!Revisited!(uva_10537)

Code:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<queue>
 5 #include<algorithm>
 6 #include<iostream>
 7 #define ll long long
 8 #define mp(x,y) make_pair(x,y)
 9 using namespace std;
10 inline int in(){
11     int x=0;bool f=0; char c;
12     for (;(c=getchar())<'0'||c>'9';f=c=='-');
13     for (x=c-'0';(c=getchar())>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0');
14     return f?-x:x;
15 }
16 struct edge{
17     int to,next;
18 }e[2750];
19 int head[127],pre[127];
20 ll dis[127];
21 int cnt=0,p,s,t,n,cs=0;
22 char ch;
23 typedef pair<ll,int> P;
24 inline void ins(int x,int y){
25     e[++cnt].to=y;e[cnt].next=head[x];head[x]=cnt;
26 }
27 inline ll ct(int u){
28     if (u>='a'&&u<='z') return 1ll*(dis[u]+1);
29     if (u>='A'&&u<='Z') return 1ll*(dis[u]%19?dis[u]/19*20+1+dis[u]%19:dis[u]/19*20);
30 }
31 void dijkstra(int s){
32     priority_queue<P,vector<P>,greater<P> >q;
33     memset(dis,127/3,sizeof(dis));dis[s]=1ll*p;q.push(mp(1ll*p,s));
34     while (!q.empty()){
35         P p=q.top();q.pop();int x=p.second;
36         if (dis[x]<p.first) continue;
37         for (int i=head[x];i;i=e[i].next){
38             int v=e[i].to;ll val=ct(x);
39             if (dis[v]>val) dis[v]=val,pre[v]=x,q.push(mp(dis[v],v));
40         }
41     }
42 }
43 int main()
44 {
45     while (~scanf("%d",&n)&&n!=-1){
46         ++cs;memset(e,0,sizeof(e));memset(pre,0,sizeof(pre));memset(head,0,sizeof(head));cnt=0;
47         for (int i=1;i<=n;++i){
48             ch=getchar();while (ch<'A'||(ch>'Z'&&ch<'a')||ch>'z') ch=getchar();s=ch;
49             ch=getchar();while (ch<'A'||(ch>'Z'&&ch<'a')||ch>'z') ch=getchar();t=ch;
50             ins(s,t);ins(t,s);
51         }p=in();
52         ch=getchar();while (ch<'A'||(ch>'Z'&&ch<'a')||ch>'z') ch=getchar();s=ch;
53         ch=getchar();while (ch<'A'||(ch>'Z'&&ch<'a')||ch>'z') ch=getchar();t=ch;
54         dijkstra(t);printf("Case %d:
%lld
%c",cs,dis[s],s);
55         for (int i=pre[s];i;i=pre[i]) printf("-%c",i);printf("
");
56     }return 0;
57 }

T4:astronauts(LA_3713)

Code:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define mst(a,n) memset(a,n,sizeof(a))
 5 #define MN 100005
 6 using namespace std;
 7 inline int in(){
 8     int x=0;bool f=0; char c;
 9     for (;(c=getchar())<'0'||c>'9';f=c=='-');
10     for (x=c-'0';(c=getchar())>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0');
11     return f?-x:x;
12 }
13 struct edge{
14     int to,next;
15 }e[MN<<2];
16 int head[MN<<1],st[MN<<1],w[MN];
17 int cnt=0,top=0,sum=0,n,m,a,b;
18 bool vis[MN<<1],sen[MN];
19 inline void ins(int x,int y){
20     e[++cnt].to=y;e[cnt].next=head[x];head[x]=cnt;
21 }
22 bool dfs(int x){
23     if (vis[x^1]) return 0;if (vis[x]) return 1;
24     vis[x]=1;st[++top]=x;
25     for (int i=head[x];i;i=e[i].next){
26         int v=e[i].to;
27         if (!dfs(v)) return 0;
28     }return 1;
29 } 
30 bool twosat(){
31     for (int i=2;i<(n<<1)+2;i+=2)
32     if (!vis[i]&&!vis[i^1]){
33         top=0;if (!dfs(i)){
34             while (top) vis[st[top]]=0,--top;
35             if (!dfs(i+1)) return 0;
36         }
37     }return 1;
38 }
39 int main()//note: a a<<1 (not a) a<<1|1
40 {
41     while (scanf("%d%d",&n,&m)&&n&&m){
42         mst(w,0);mst(sen,0);mst(e,0);
43         mst(st,0);mst(head,0);mst(vis,0);cnt=sum=top=0;
44         for (int i=1;i<=n;++i) w[i]=in(),sum+=w[i];
45         for (int i=1;i<=n;++i) sen[i]=(w[i]*n>=sum);
46         for (int i=1;i<=m;++i){
47             a=in();b=in();
48             if (sen[a]^sen[b]) {ins(a<<1|1,b<<1);ins(b<<1|1,a<<1);}
49             else {
50                 ins(a<<1|1,b<<1);ins(a<<1,b<<1|1);
51                 ins(b<<1|1,a<<1);ins(b<<1,a<<1|1);
52             }
53         }if (!twosat()) {printf("No solution.
");continue;};
54         for (int i=1;i<=n;++i) {
55             if (vis[i<<1]) printf("%c
",sen[i]?'A':'B');
56             else printf("C
");
57         }
58     }
59     return 0;
60 }

T5:Priest John’s Busiest Day(poj_3683)

Code:

原文地址:https://www.cnblogs.com/codingutopia/p/2017summer_day1.html