gym/101873/GCPC2017
题目链接:https://codeforces.com/gym/101873
C. Joyride
记忆化搜索形式的dp
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> //#include <unordered_map> /* ⊂_ヽ \\ Λ_Λ 来了老弟 \('ㅅ') > ⌒ヽ / へ\ / / \\ レ ノ ヽ_つ / / / /| ( (ヽ | |、\ | 丿 \ ⌒) | | ) / 'ノ ) Lノ */ using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef long double ld; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0) #define rep(a, b, c) for(int a = (b); a <= (c); ++ a) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 998244353; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } inline void cmax(int &x,int y){if(x<y)x=y;} inline void cmax(ll &x,ll y){if(x<y)x=y;} inline void cmin(int &x,int y){if(x>y)x=y;} inline void cmin(ll &x,ll y){if(x>y)x=y;} #define MODmul(a, b) ((a*b >= mod) ? ((a*b)%mod + 2*mod) : (a*b)) #define MODadd(a, b) ((a+b >= mod) ? ((a+b)%mod + 2*mod) : (a+b)) /*-----------------------showtime----------------------*/ const int maxn = 1e3+9; int dp[maxn][maxn]; vector<int>mp[maxn]; int x,n,m,T; int tim[maxn],cost[maxn]; int dfs(int u,int t){ if(t > x) return inf; if(dp[u][t]!=-1) return dp[u][t]; if(u == 1 && t == x) return dp[u][t] = 0; dp[u][t] = inf; for(int i=0; i<mp[u].size(); i++){ int v = mp[u][i]; dp[u][t] = min(dp[u][t], dfs(v, t + T + tim[v]) + cost[v]); } dp[u][t] = min(dp[u][t], dfs(u, t+tim[u]) + cost[u]); return dp[u][t]; } int main(){ memset(dp, -1, sizeof(dp)); scanf("%d", &x); scanf("%d%d%d", &n, &m, &T); rep(i, 1, m) { int u,v; scanf("%d%d", &u, &v); mp[u].pb(v); mp[v].pb(u); } rep(i, 1, n) { scanf("%d%d", &tim[i], &cost[i]); } int ans = dfs(1, tim[1]) + cost[1]; if(ans < inf) printf("%d\n", ans); else puts("It is a trap."); return 0; }
E.Perpetuum Mobile
找正环,由于是乘法,所以用log变成加法,还有这题其实是看爱因斯坦那句话,和他助手说的没什么关系,所以要找一个环,环上的乘积大于1,即log >0。于是用spfa判正环就行了。
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> //#include <unordered_map> /* ⊂_ヽ \\ Λ_Λ 来了老弟 \('ㅅ') > ⌒ヽ / へ\ / / \\ レ ノ ヽ_つ / / / /| ( (ヽ | |、\ | 丿 \ ⌒) | | ) / 'ノ ) Lノ */ using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef long double ld; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0) #define rep(a, b, c) for(int a = (b); a <= (c); ++ a) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 998244353; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } inline void cmax(int &x,int y){if(x<y)x=y;} inline void cmax(ll &x,ll y){if(x<y)x=y;} inline void cmin(int &x,int y){if(x>y)x=y;} inline void cmin(ll &x,ll y){if(x>y)x=y;} #define MODmul(a, b) ((a*b >= mod) ? ((a*b)%mod + 2*mod) : (a*b)) #define MODadd(a, b) ((a+b >= mod) ? ((a+b)%mod + 2*mod) : (a+b)) /*-----------------------showtime----------------------*/ typedef pair<int,double> pid; const int maxn = 1e3+9; vector<pid>mp[maxn]; int vis[maxn]; double dis[maxn]; queue<int>que; bool spfa(int u){ vis[u] = true; for(int i=0; i<mp[u].size(); i++){ pid tmp = mp[u][i]; int v = tmp.fi; double w = tmp.se; if(dis[v] < dis[u] + w){ dis[v] = dis[u] + w; if(vis[v]) return true; if(spfa(v)) return true; } } vis[u] = false; return false; } int main(){ int n,m; scanf("%d%d", &n, &m); rep(i, 1, m) { int u,v; double e; scanf("%d%d%lf", &u, &v, &e); mp[u].pb(pid(v, log(e))); } for (int i = 1; i <= n; ++i) if(spfa(i)) return 0*puts("inadmissible"); puts("admissible"); return 0; }
H - Ratatoskr
枚举每个点作为根节点的最大深度,如果这个节点原本就有乌鸦,就只能考虑松鼠所在子树的深度,如果这个节点原本没有乌鸦,就只用考虑这棵树的深度
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> //#include <unordered_map> /* ⊂_ヽ \\ Λ_Λ 来了老弟 \('ㅅ') > ⌒ヽ / へ\ / / \\ レ ノ ヽ_つ / / / /| ( (ヽ | |、\ | 丿 \ ⌒) | | ) / 'ノ ) Lノ */ using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef long double ld; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0) #define rep(a, b, c) for(int a = (b); a <= (c); ++ a) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 998244353; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } inline void cmax(int &x,int y){if(x<y)x=y;} inline void cmax(ll &x,ll y){if(x<y)x=y;} inline void cmin(int &x,int y){if(x>y)x=y;} inline void cmin(ll &x,ll y){if(x>y)x=y;} #define MODmul(a, b) ((a*b >= mod) ? ((a*b)%mod + 2*mod) : (a*b)) #define MODadd(a, b) ((a+b >= mod) ? ((a+b)%mod + 2*mod) : (a+b)) /*-----------------------showtime----------------------*/ const int maxn = 109; vector<int>mp[maxn]; int dp[maxn],vis[maxn]; int s,b1,b2,n; void dfs(int u,int fa){ dp[u] = 1; if(u == s) vis[u] = 1; for(int i=0; i<mp[u].size(); i++){ int v = mp[u][i]; if(v == fa) continue; dfs(v, u); dp[u] = max(dp[u], dp[v] + 1); if(vis[v]) vis[u] = 1; } } int main(){ scanf("%d%d%d%d", &n, &s, &b1, &b2); rep(i, 1, n-1) { int u,v; scanf("%d%d", &u, &v); mp[u].pb(v); mp[v].pb(u); } int ans = inf; for(int i=1; i<=n; i++) { if(i == b1 || i ==b2){ memset(vis, 0, sizeof(vis)); dfs(i, -1); for(int j=0; j<mp[i].size(); j++) { int v = mp[i][j]; if(vis[v]) ans = min(ans, dp[v]); } } else { dfs(i, -1); ans = min(ans,dp[i]); } } printf("%d\n", ans); return 0; }
J-Word Clock
状压搜索,注意dfs的次序,还有就是只要关了同步,就不能用scanf
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> /* ⊂_ヽ \\ Λ_Λ 来了老弟 \('ㅅ') > ⌒ヽ / へ\ / / \\ レ ノ ヽ_つ / / / /| ( (ヽ | |、\ | 丿 \ ⌒) | | ) / 'ノ ) Lノ */ using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0) #define rep(a, b, c) for(int a = (b); a <= (c); ++ a) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } inline void cmax(int &x,int y){if(x<y)x=y;} inline void cmax(ll &x,ll y){if(x<y)x=y;} inline void cmin(int &x,int y){if(x>y)x=y;} inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ int n,h,w; string str[22],t[22]; pii dp[20][1<<18],nxt[20][1<<18]; int dis[22][22]; pii dfs(int u,int state){ if(dp[u][state].fi != -1 && dp[u][state].se != -1) return dp[u][state]; if(state == (1<<n) - 1) return dp[u][state] = pii(1,t[u].length()); pii ans = pii(inf, inf); for(int i=1; i<=n; i++){ if(!(state & (1 << (i-1)))) { pii tmp; tmp = dfs(i, state | (1<<(i-1))); int he,wi; int cen = tmp.fi; if(tmp.se + dis[u][i] > w) { he = tmp.fi + 1; wi = t[u].length(); } else { he = tmp.fi; wi = tmp.se + dis[u][i]; } if(he < ans.fi || (he==ans.fi && ans.se > wi)) { ans.fi = he; ans.se = wi; nxt[u][state] = pii(i, cen); } } } return dp[u][state] = ans; } bool cmp(string a,string b){ return a.length() < b.length(); } vector<int> vec[22]; int main(){ //scanf("%d%d%d", &h, &w, &n); boost; cin>>h>>w>>n; memset(dis, 0, sizeof(dis)); memset(dp, -1, sizeof(dp)); int sum = 0; for(int i=1; i<=n; i++) cin>>str[i],sum+=str[i].length(); sort(str+1, str+1+n, cmp); int tot = 0; for(int i=1; i<=n; i++){ if(str[i].length() > w) { puts("impossible"); return 0; } int flag = 1; for(int j=i+1; j<=n; j++){ if(str[j].find(str[i]) != string::npos) flag = 0; } if(flag) t[++tot] = str[i]; } n = tot; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++){ int l1 = t[i].length(), l2 = t[j].length(); int len = min(l1, l2); for(int k=0; k<=len; k++){ if(t[i].substr(l1-k,k) ==t[j].substr(0, k)) dis[i][j] = l1 - k; } } } pii tmp = dfs(0, 0); // cout<<tmp.fi<<" , "<<tmp.se<<endl; if(tmp.fi>h || tmp.se > w) return 0*puts("impossible"); int st = 0,u = 0; while(st < (1<<n) - 1) { int id = nxt[u][st].fi; // cout<<id<<" "<<nxt[u][st].se<<endl; vec[nxt[u][st].se].pb(id); u = id; st |= (1 << (id - 1)); } for(int i=1; i<=h; i++) { string ans = ""; for(int j=0; j<vec[i].size(); j++){ string tmp = t[vec[i][j]]; int l1 = ans.length(),l2 = tmp.length(); int len = min(l1, l2),q; for(int k=0; k<=len; k++){ if(ans.substr(l1-k,k) ==tmp.substr(0, k)) q = k; } for(int k=q; k<l2; k++) ans += tmp[k]; } while(ans.length() < w) ans += "A"; cout<<ans<<endl; } return 0; } /* 5 10 12 ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE TEN ELEVEN TWELVE 5 10 12 UNO DUE TRE QUATTRO CINQUE SEI SETTE OTTO NOVE DIECI UNDICI DODICI */
skr