PAT甲级1013-1014-1015

题目1013 Battle Over Cities

思路:城市数也就1000, 对于每次询问暴力bfs一下看一下有多少连通块就行了。答案就是联通块数减一。

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<map>
 4 #include<set>
 5 #include<iostream>
 6 #include<cstring>
 7 #include<algorithm>
 8 #include<vector>
 9 #include<cmath> 
10 #include<queue>
11 
12 #define inf 0x7fffffff
13 using namespace std;
14 typedef long long LL;
15 typedef pair<int, int> pr;
16 
17 int n, m, k;
18 const int maxm = 1e6 + 5;
19 const int maxn = 1005;
20 struct edge{
21     int u, v, nxt;
22 }e[maxm * 2];
23 int head[maxn], tot = 1;
24 
25 void addedge(int u, int v)
26 {
27     e[tot].u = u;
28     e[tot].v = v;
29     e[tot].nxt = head[u];
30     head[u] = tot++;
31     e[tot].u = v;
32     e[tot].v = u;
33     e[tot].nxt = head[v];
34     head[v] = tot++;    
35 } 
36  
37 int vis[maxn];
38 void bfs(int c, int city)
39 {
40     queue<int>que;
41     que.push(c);
42     vis[c] = true;
43     while(!que.empty()){
44         int now = que.front();que.pop();
45         for(int ed = head[now]; ed != -1; ed = e[ed].nxt){
46             if(e[ed].v != city && !vis[e[ed].v]){
47                 vis[e[ed].v] = true;
48                 que.push(e[ed].v);
49             }
50         }
51     }
52     return ;
53 }
54 
55 int main()
56 {
57     scanf("%d%d%d", &n, &m, &k);
58     memset(head, -1, sizeof(head));
59     for(int i = 0; i < m; i++){
60         int u, v;
61         scanf("%d%d", &u, &v);
62         addedge(u, v);
63     }
64     for(int i = 0; i < k; i++){
65         int city;
66         scanf("%d", &city);
67         memset(vis, 0, sizeof(vis));
68         int cnt = 0;
69         for(int i = 1; i <= n; i++){
70             if(!vis[i] && i != city){
71                 bfs(i, city);
72                 cnt++;
73             }
74         }
75         printf("%d\n", cnt - 1);
76     }
77     return 0;
78 }

题目1014 Waiting in Line

思路:大模拟。我好菜系列。

用队列模拟每个窗口排队的人。没满的时候就是从左到右排就行了,满了之后就是哪个队伍先有人走哪个队伍就先进去。这里用优先队列模拟。

坑点是,如果在五点之前被服务了,服务时间超过五点也没关系,但是如果开始服务时间就已经超过五点了就要Sorry。

还有一个地方写错了是刚开始所有人都排进去就直接结束了,但是队列里其实还有人。

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<map>
  4 #include<set>
  5 #include<iostream>
  6 #include<cstring>
  7 #include<algorithm>
  8 #include<vector>
  9 #include<cmath> 
 10 #include<queue>
 11 
 12 #define inf 0x7fffffff
 13 using namespace std;
 14 typedef long long LL;
 15 typedef pair<int, int> pr;
 16 
 17 int n, m, k, q;
 18 const int maxn = 1005;
 19 int process[maxn], total[maxn];
 20 queue<int>que[25];
 21 int t[25];
 22 
 23 struct node{
 24     int time, line;
 25     node(){
 26     }
 27     node(int t, int l)
 28     {
 29         time = t;
 30         line = l;
 31     }
 32     bool operator < (const node& a)const{
 33         if(time == a.time)return line > a.line; 
 34         return time > a.time;
 35     }
 36 };
 37 
 38 int main()
 39 {
 40     scanf("%d%d%d%d", &n, &m, &k, &q);
 41     for(int i = 1; i <= k; i++){
 42         scanf("%d", &process[i]);
 43     }
 44     
 45     int id = 1;
 46     priority_queue<node>lineque;
 47     for(int i = 1; i <= m; i++){
 48         for(int j = 1; j <= n; j++){
 49             que[j].push(id++);
 50             if(id > k)break;
 51         }
 52         if(id > k)break;
 53     }
 54     
 55     //que[1].push(id++);
 56     
 57 //    for(int i = 1; i <= n; i++){
 58 //        lineque.push(node(0, i));
 59 //    }
 60     int sum = k;
 61     while(id <= k){
 62         //int linetime = 10000, line;
 63         for(int i = 1; i <= n; i++){
 64             if(!que[i].empty()){
 65                 int peo = que[i].front();que[i].pop();
 66                 sum--;
 67                 t[i] += process[peo];
 68                 total[peo] = t[i];
 69                 lineque.push(node(total[peo], i));
 70             }
 71 //            if(linetime > t[i]){
 72 //                linetime = t[i];
 73 //                line = i;
 74 //            }
 75         }
 76 //        que[line].push(id++);
 77         
 78         node l = lineque.top();lineque.pop();
 79         que[l.line].push(id++);
 80     }
 81     
 82     while(sum){
 83         for(int i = 1; i <= n; i++){
 84             if(!que[i].empty()){
 85                 int peo = que[i].front();que[i].pop();
 86                 sum--;
 87                 t[i] += process[peo];
 88                 total[peo] = t[i];
 89             }
 90         }
 91     }
 92     
 93     for(int i = 0; i < q; i++){
 94         int p;
 95         scanf("%d", &p);
 96         int h = total[p] / 60;
 97         int min = total[p] % 60;
 98         //printf("%d:%d\n",p,total[p]);
 99         if(h + 8 >= 17 && min != 0 && total[p] - process[p] >= 540){
100             printf("Sorry\n");
101         }
102         else{
103             printf("%02d:%02d\n", h + 8, min);
104         }
105     }
106     return 0;
107 }

猜你喜欢

转载自www.cnblogs.com/wyboooo/p/10408250.html