作了一段時間的CCF CSP試題,我的感受是這樣分佈的c++
#include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<b;i++) #define dd(x) cout<<#x<<'='<<x<<", " #define de(x) cout<<#x<<'='<<x<<endl typedef long long ll; const int maxn = 1e5+5; int num[maxn]; int main() { int n,m; scanf("%d%d",&n,&m); rep(i,1,n+1) num[i] = i; while(m--) { int p,q; scanf("%d%d",&p,&q); int pos = 1; while(num[pos]!=p) pos++; if(q>0){ for(int i=pos;i<pos+q;i++) num[i] = num[i+1]; num[pos+q] = p; } else{ for(int i=pos;i>pos+q;i--) num[i] = num[i-1]; num[pos+q] = p; } } rep(i,1,n+1) printf("%d%c",num[i],i==n?'\n':' '); }
#include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<b;i++) #define dd(x) cout<<#x<<'='<<x<<", " #define de(x) cout<<#x<<'='<<x<<endl typedef long long ll; const int maxn = 1e5+5; int num[maxn]; int main() { int n,m; scanf("%d%d",&n,&m); rep(i,1,n+1) num[i] = i; while(m--) { int p,q; scanf("%d%d",&p,&q); int pos = 1; while(num[pos]!=p) pos++; if(q>0){ for(int i=pos;i<pos+q;i++) num[i] = num[i+1]; num[pos+q] = p; } else{ for(int i=pos;i>pos+q;i--) num[i] = num[i-1]; num[pos+q] = p; } } rep(i,1,n+1) printf("%d%c",num[i],i==n?'\n':' '); }
#include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<b;i++) #define dd(x) cout<<#x<<'='<<x<<", " #define de(x) cout<<#x<<'='<<x<<endl typedef long long ll; const int maxn = 1e5+5; const int INF = 0x3f3f3f3f; void open(){freopen("data.txt", "r", stdin);} // 單獨處理每一行 string format_line(string str) { int len = str.size(); string res; rep(i,0,len){ // 處理強調 if(str[i]=='_'){ int j = i+1; while(str[j]!='_') j++; // 遞歸處理 res = str.substr(0, i).append("<em>").append(format_line(str.substr(i+1, j-i-1))).append("</em>").append(format_line(str.substr(j+1, len-j-1))); return res; } // 處理超連接 else if(str[i]=='['){ int j = i+1; while(str[j]!=']') j++; int p = j+1; int q = p+1; while(str[q]!=')') q++; // 遞歸處理 res = str.substr(0, i).append("<a href=\"").append(format_line(str.substr(p+1, q-p-1))).append("\">").append(format_line(str.substr(i+1, j-i-1))).append("</a>").append(format_line(str.substr(q+1, len-q-1))); return res; } } return str; } // 處理#打頭的行 void func_comment(string line) { int len = line.size(); int cnt = 0; int p = 0; while(line[p]=='#' && p<len){ cnt++; p++; } while(line[p]==' ' && p<len) p++; cout<<"<h"<<cnt<<">"<<format_line(line.substr(p, len-p))<<"</h"<<cnt<<">"<<endl; // 尋找第一個不是空格的位置 } // 處理*打頭的行 void func_star(string line) { int len = line.size(); int p = 0; while(line[p]==' ' || line[p]=='*') p++; cout<<"<li>"<<format_line(line.substr(p,len-p))<<"</li>"<<endl; } int main() { //open(); string line; while(getline(cin, line)) { bool newp = true; bool newlist = true; if(line.size()==0){ //puts("00000"); continue; } else if(line[0]=='#'){ //puts("######"); func_comment(line); } else if(line[0]=='*'){ //puts("*******"); puts("<ul>"); func_star(line); // 接二連三輸入 直處處理完無序列表 while(getline(cin, line)) { if(line.size()>0 && line[0]=='*') func_star(line); else break; } puts("</ul>"); } else{ //puts("ppppppppp"); cout<<"<p>"; cout<<format_line(line); bool flag = false; while(getline(cin, line)){ if(line.size()==0){ flag = true; cout<<"</p>"<<endl; break; } cout<<endl; cout<<format_line(line); } if(!flag) cout<<"</p>"<<endl; } } }
dist[v] = dist[u] + cost
變成dist[v] = max(dist[u], cost)
#include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<b;i++) #define dd(x) cout<<#x<<'='<<x<<", " #define de(x) cout<<#x<<'='<<x<<endl typedef long long ll; const int maxn = 1e5+5; const int INF = 0x3f3f3f3f; struct Edge { int v; int cost; Edge(int _v=0, int _cost=0):v(_v), cost(_cost){} }; vector<Edge> E[maxn]; void addedge(int u,int v,int w) { E[u].push_back(Edge(v,w)); } bool vis[maxn]; int cnt[maxn]; int dist[maxn]; bool SPFA(int start, int n) { memset(vis, 0, sizeof(vis)); rep(i,1,n+1) dist[i] = INF; vis[start] = true; dist[start] = 0; queue<int> que; while(!que.empty()) que.pop(); que.push(start); cnt[start] = 1; while(!que.empty()) { int u = que.front(); que.pop(); vis[u] = false; rep(i,0,E[u].size()){ int v = E[u][i].v; int cost = E[u][i].cost; // 這裏修改了兩行 if(dist[v]>max(dist[u],cost)){ dist[v] = max(dist[u], cost); if(!vis[v]){ vis[v] = true; que.push(v); if(++cnt[v]>n) return false; } } } } return true; } int main() { int n,m; scanf("%d%d",&n,&m); int u,v,w; while(m--) { scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); addedge(v,u,w); } SPFA(1,n); printf("%d\n", dist[n]); }