【模拟】NCPC 2014 K Train passengers

题目链接:

  http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1797

题目大意:

  有N个车站,火车一共可以坐M个人,每个车站下车Ai,上车Bi个人,在车站等待下一班Ci个人。问输入是否合法。

  合法:火车上的人不超过M,第一站不能有人下车,最后一站不能有人上车,火车满的时候才能有人在车站等下一班。

题目思路:

  【图论】

  签到水题。模拟到达每个车站的状态即可。

 1 //
 2 //by coolxxx
 3 //#include<bits/stdc++.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<map>
 9 #include<stack>
10 #include<queue>
11 #include<set>
12 #include<bitset>
13 #include<memory.h>
14 #include<time.h>
15 #include<stdio.h>
16 #include<stdlib.h>
17 #include<string.h>
18 //#include<stdbool.h>
19 #include<math.h>
20 #define min(a,b) ((a)<(b)?(a):(b))
21 #define max(a,b) ((a)>(b)?(a):(b))
22 #define abs(a) ((a)>0?(a):(-(a)))
23 #define lowbit(a) (a&(-a))
24 #define sqr(a) ((a)*(a))
25 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
26 #define mem(a,b) memset(a,b,sizeof(a))
27 #define eps (1e-8)
28 #define J 10
29 #define mod 1000000007
30 #define MAX 0x7f7f7f7f
31 #define PI 3.14159265358979323
32 #define N 104
33 using namespace std;
34 typedef long long LL;
35 int cas,cass;
36 int n,m,lll,ans;
37 int a[N],b[N],c[N];
38 bool work()
39 {
40     int i,j,sum=0;
41     for(i=1;i<=n;i++)
42     {
43         sum-=a[i];
44         if(sum<0)return 0;
45         sum+=b[i];
46         if(sum>m)return 0;
47         if(c[i]>0 && sum!=m)return 0;
48     }
49     if(b[n]!=0 || c[n]!=0 || sum!=0)return 0;
50     return 1;
51 }
52 int main()
53 {
54     #ifndef ONLINE_JUDGE
55 //    freopen("1.txt","r",stdin);
56 //    freopen("2.txt","w",stdout);
57     #endif
58     int i,j,k;
59 //    for(scanf("%d",&cass);cass;cass--)
60 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
61 //    while(~scanf("%s",s+1))
62     while(~scanf("%d",&m))
63     {
64         scanf("%d",&n);
65         ans=0;
66         for(i=1;i<=n;i++)
67             scanf("%d%d%d",&a[i],&b[i],&c[i]);
68         if(work())puts("possible");
69         else puts("impossible");
70     }
71     return 0;
72 }
73 /*
74 //
75 
76 //
77 */
View Code
原文地址:https://www.cnblogs.com/Coolxxx/p/5827289.html