diff --git a/8.15/B-House Man/Readme.md b/8.15/B-House Man/Readme.md index de262f7..bb840fe 100644 --- a/8.15/B-House Man/Readme.md +++ b/8.15/B-House Man/Readme.md @@ -1,12 +1,17 @@ -# 题目 -* 链接 +# B - House Man +* https://vjudge.net/contest/509525#problem/B ### 题意 - +给定有一定顺序的一组数,要求从小到大走,最大跨D这么远即A到B中间最多隔着(D-1)个,问能否走到,最小和最大的最大距离多少 ### 做法 - +差分约束(1.原来顺序左到右>=1;2.小到大再按原来的左到右<=D) +max,min中原来id小的做S,id大的做T;dist[T]就是答案 ### 关键词 - +差分约束,构图,源点,汇点 ### 易错点 - +构图,还是不是很理解 +//TLE 判断负环的(been/times)放错位置 +cnt=0;//WA罪魁祸首! 没有每次赋零 +scanf("%d",&a[i].h);//怎么总是你&!! + wa=0;//WA? bool标记忘了每次赋零 ### 工具箱 * \ No newline at end of file diff --git a/8.15/F - Candies.cpp b/8.15/F - Candies.cpp deleted file mode 100644 index a58a8e3..0000000 --- a/8.15/F - Candies.cpp +++ /dev/null @@ -1,70 +0,0 @@ -//youwu -#include -#include -#include -#include -#include -using namespace std; -const int N=3e4+5,N3=15e4+5,inf=0x3f3f3f3f; -int to[N3],ne[N3],w[N3],h[N],cnt=0,s=N-4,e,dist[N]; -bool in[N],wa=0,u,v,w1; -int n,m,maxn=0; -void add(int u,int v,int w1) -{ - to[++cnt]=v; - ne[cnt]=h[u]; - w[cnt]=w1; - h[u]=cnt; -} -void spfa() -{ - stackq;//queue没有自带函数清零 - q.push(s); - while(!q.empty()) - { - int u=q.top(); - q.pop(); - in[u]=0;//spfa的精髓!! - for(int i=h[u];i;i=ne[i]) - { - int v=to[i],w1=w[i]; -// printf("%d %d %d\n",u,v,w1); - if(dist[u]+w1maxn) -// { -// maxn=dist[i]; -// } -// } - printf("%d",dist[n]); - return 0; -} diff --git a/8.15/F-Candies/F.pdf b/8.15/F-Candies/F.pdf new file mode 100644 index 0000000..4587eb5 --- /dev/null +++ b/8.15/F-Candies/F.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:43ff1136a808355216623bd7a5ff166cc410d6be6ef0f836c2aa281626ee5e63 +size 82802 diff --git a/8.15/F-Candies/doc/Readme.md b/8.15/F-Candies/doc/Readme.md new file mode 100644 index 0000000..4e768b5 --- /dev/null +++ b/8.15/F-Candies/doc/Readme.md @@ -0,0 +1 @@ +# \ No newline at end of file diff --git a/8.15/F-Candies/main.cpp b/8.15/F-Candies/main.cpp new file mode 100644 index 0000000..b27b54f --- /dev/null +++ b/8.15/F-Candies/main.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +using namespace std; +const int N = 3e4 + 5, N3 = 15e4 + 5, inf = 0x3f3f3f3f; +int to[N3], ne[N3], w[N3], h[N], cnt = 0, s = N - 4, e, dist[N]; +bool in[N], wa = 0; +int u, v, w1;//WA 和bool放一起了 +int n, m, maxn = 0; +void add(int u, int v, int w1) +{ + to[++cnt] = v; + ne[cnt] = h[u]; + w[cnt] = w1; + h[u] = cnt; +} +void spfa(int s) +{ + memset(dist,0x3f,sizeof dist);//更好的賦初值方法 + dist[s] = 0;//和賦inf顺序反了=白干 + stack q; // queue没有自带函数清零 + q.push(s); + while (!q.empty()) + { + u = q.top(); + q.pop(); + in[u] = 0; // spfa的精髓!! + for (int i = h[u]; i; i = ne[i]) + { + v = to[i], w1 = w[i]; + // printf("%d %d %d\n",u,v,w1); + if (dist[u] + w1 < dist[v]) + { + dist[v] = dist[u] + w1; + // printf("%d(%d) %d(%d) %d\n",u,dist[u],v,dist[v],w1); + if (!in[v]) + { + q.push(v); + in[v] = 1; + } + } + } + } +} +int main() +{ + scanf("%d%d", &n, &m); + // for (int i = 0; i <= n; i++) + // { + // dist[i] = inf; + // } + for (int i = 1; i <= m; i++) // n,m不分 + { + scanf("%d%d%d", &u, &v, &w1); + add(u, v, w1); + } + spfa(1); + // maxn = 0; + // for(int i=2;i<=n;i++) + // { + // if(dist[i]>maxn) + // { + // maxn=dist[i]; + // } + // } + printf("%d", dist[n]); + return 0; +} diff --git a/8.15/F-Candies/reference.cpp b/8.15/F-Candies/reference.cpp new file mode 100644 index 0000000..e1b45d6 --- /dev/null +++ b/8.15/F-Candies/reference.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +using namespace std; + +typedef long long LL; +typedef pair PII; +const int INF = 0x3f3f3f3f; + +const int N = 30005, M = 150005; + +inline int read() +{ + register int x = 0, f = 1; + register char ch = getchar(); + while(ch < '0' || ch > '9') + { + if(ch == '-') f = -1; + ch = getchar(); + } + while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); + return x * f; +} + +int n, m; +struct Edge +{ + int v,nxt,w; +}e[M]; +int head[N], idx = 0; +inline void add(int u,int v,int w=0) +{ + e[++idx].v = v,e[idx].w = w, e[idx].nxt = head[u],head[u] = idx; +} + +int dis[N], st[N], times[N]; +bool spfa(int start) +{ + stack q; + memset(dis,0x3f,sizeof dis); + dis[start] = 0, st[start] = times[start] = 1, q.push(start); + + while(!q.empty()) + { + int u = q.top(); q.pop(); + + st[u] = 0; + for(int i=head[u];i;i=e[i].nxt) + { + int v = e[i].v, w = e[i].w; + if(dis[v] > dis[u] + w) + { + dis[v] = dis[u] + w; + if(!st[v]) + { + st[v] = 1, q.push(v); + // if(times[v] >= n) return 0; + } + } + } + } + + return 1; +} + +int main() +{ + n = read(), m = read(); + for(int i=1;i<=m;i++) + { + int u = read(),v = read(),w = read(); + add(u,v,w); + } + + spfa(1); + cout<