SGU 103 Traffic Lights
2012-03-18 13:16
393 查看
wa没有关系,冷静的找到wa的原因才是关键~
这题我想复杂了,以为过马路的时候灯的颜色要一直相同,而且相同的时间要大于过马路需要的时间,那样就是个数论问题,取两个点循环时间的最大公约数,约掉之后求任意时刻相交的最大时间,挺麻烦的,然后再看了一下题,原来是只要有一个时刻颜色相同就能过去,这样就简单了不知道多少了,只要求下一个颜色相同的时间就可以了,轻松ac~
这题我想复杂了,以为过马路的时候灯的颜色要一直相同,而且相同的时间要大于过马路需要的时间,那样就是个数论问题,取两个点循环时间的最大公约数,约掉之后求任意时刻相交的最大时间,挺麻烦的,然后再看了一下题,原来是只要有一个时刻颜色相同就能过去,这样就简单了不知道多少了,只要求下一个颜色相同的时间就可以了,轻松ac~
#include<iostream> #include<vector> #include<algorithm> #include<cstdio> #include<queue> #include<stack> #include<string> #include<map> #include<set> #include<cmath> #include<cassert> #include<cstring> #include<iomanip> using namespace std; #ifdef _WIN32 #define i64 __int64 #define out64 "%I64d\n" #define in64 "%I64d" #else #define i64 long long #define out64 "%lld\n" #define in64 "%lld" #endif #define FOR(i,a,b) for( int i = (a) ; i <= (b) ; i ++) #define FF(i,a) for( int i = 0 ; i < (a) ; i ++) #define FFD(i,a) for( int i = (a)-1 ; i >= 0 ; i --) #define S64(a) scanf(in64,&a) #define SS(a) scanf("%d",&a) #define LL(a) ((a)<<1) #define RR(a) (((a)<<1)+1) #define SZ(a) ((int)a.size()) #define PP(n,m,a) puts("---");FF(i,n){FF(j,m)cout << a[i][j] << ' ';puts("");} #define pb push_back #define CL(Q) while(!Q.empty())Q.pop() #define MM(name,what) memset(name,what,sizeof(name)) #define read freopen("in.txt","r",stdin) #define write freopen("out.txt","w",stdout) const int inf = 0x3f3f3f3f; const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL; const double oo = 10e9; const double eps = 10e-10; const double pi = acos(-1.0); const int maxn = 333; struct zz { int from; int to; int cost; }zx; int dp[maxn]; bool inq[maxn]; int back[maxn]; int s,t,n,m; vector<zz>g[maxn]; queue<int>q; int col[maxn]; int re[maxn]; int b[maxn]; int p[maxn]; char c; vector<int>sta; int find(int tx,int i) { tx -= re[i]; if(tx < 0) { return col[i]; } if(tx == 0) { return -col[i]; } tx %= b[i] + p[i]; if(col[i] == -1) { if( tx < b[i] ) { return 1; } else { return -1; } } else if(col[i] == 1) { if(tx < p[i]) { return -1; } else { return 1; } } } int next(int tx,int i) { tx -= re[i]; if(tx < 0) { return -tx; } if(tx==0) { if(col[i] == 1) { return p[i]; } else { return b[i]; } } tx %= b[i] + p[i]; if(col[i] == -1) { if( tx < b[i] ) { return b[i] - tx; } else { return b[i] + p[i] - tx; } } else if(col[i] == 1) { if(tx < p[i]) { return p[i] - tx; } else { return p[i] + b[i] - tx; } } } int fuck(int tn,int now,int to) { int c1,c2; int n1,n2; int temp; c1 = find(tn,now); n1 = next(tn,now); n2 = next(tn,to); if(n1 != n2) { temp = min(n1,n2); return temp; } else { if(c1 == 1) { if(p[now] != b[to]) { temp = min(p[now],b[to]); temp += n1; return temp; } else { if(b[now] != p[to]) { temp = min(b[now],p[to]); temp += n1 + p[now]; return temp; } else { return inf; } } } else { if(b[now] != p[to]) { temp = min(b[now],p[to]); temp += n1; return temp; } else { if(p[now] != b[to]) { temp = min(p[now],b[to]); temp += b[now] + n1; return temp; } else { return inf; } } } } assert(false); } int wait(int tx,int now,int to) { int c1,c2; int n1,n2; c1 = find(tx,now); c2 = find(tx,to); if(c1 == c2) { return 0; } else { return fuck(tx,now,to); } } int spfa() { q.push(s); inq[s] = true; int now,to,cost,temp; while(!q.empty()) { now = q.front(); q.pop(); FF(i,g[now].size()) { to = g[now][i].to; if(to == now) { continue; } temp = wait(dp[now],now,to); cost = dp[now] + g[now][i].cost + temp; if(cost < dp[to]) { back[to] = now; dp[to] = cost; if(!inq[to]) { q.push(to); inq[to] = true; } } } inq[now] = false; } return dp[t]; } void start() { while(!q.empty()) { q.pop(); } MM(inq,false); for(int i=1;i<=n;i++) { dp[i] = inf; } dp[s] = 0; MM(back,-1); int ans=spfa(); if(ans == inf) { cout<<"0"<<endl; } else { int temp = t; sta.clear(); while(back[temp] != -1) { sta.pb(temp); temp = back[temp]; } cout<<ans<<endl; cout<<s; while(!sta.empty()) { cout<<" "<<sta.back(); sta.pop_back(); } cout<<endl; } return ; } int main() { while(cin>>s>>t) { cin>>n>>m; FF(i,maxn) { g[i].clear(); } for(int i=1;i<=n;i++) { cin>>c>>re[i]>>b[i]>>p[i]; if(c=='B') { col[i] = 1; } else if(c=='P') { col[i] = -1; } } int from,to; int cost; for(int i=1;i<=m;i++) { SS(from); SS(to); SS(cost); zx.from=from; zx.to=to; zx.cost=cost; g[zx.from].push_back(zx); swap(zx.from,zx.to); g[zx.from].push_back(zx); } start(); } return 0; }
相关文章推荐
- [SGU 107]987654321 problem (数论)
- SGU491 Game for Little Johnny
- 最小N使得N!的末尾恰有Q个0——SGU-154 Factorial
- SGU 111 Very simple problem 翻译 题解
- sgu 231 打表+二分 水过~~
- SGU 107 987654321 problem
- SGU 115 Calendar
- SGU 107 987654321 problem
- SGU 121 Bridges painting
- SGU 136 高斯消元法的特殊情况
- sgu131
- sgu 176 有源汇的上下界最小流(二分汇源的上界)(好题)
- SGU 185 Two shortest 最短路+最小费用最大流 或者 最短路+最大流
- SGU 411 Petya the Hero
- sgu 194 上下界最大流 此代码SGU瞬间过,ZOJ瞬间WA
- SGU181_X-Sequence(暴力寻找循环节)
- sgu 107 987654321 problem
- sgu 117 Counting
- sgu 109 Magic of David Copperfield II
- [SGU]135. Drawing Lines