8.15B题笔记

This commit is contained in:
ljcjames 2022-08-15 21:38:09 +08:00
parent c23a038c0f
commit 3dd15fd977
11 changed files with 173 additions and 77 deletions

View File

@ -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标记忘了每次赋零
### 工具箱
*

View File

@ -1,70 +0,0 @@
//youwu
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
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()
{
stack<int>q;//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]+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);
}
s=1;
dist[s]=0;
spfa();
maxn=0;
// for(int i=2;i<=n;i++)
// {
// if(dist[i]>maxn)
// {
// maxn=dist[i];
// }
// }
printf("%d",dist[n]);
return 0;
}

BIN
8.15/F-Candies/F.pdf (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -0,0 +1 @@
#

70
8.15/F-Candies/main.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
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<int> 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;
}

View File

@ -0,0 +1,80 @@
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
typedef long long LL;
typedef pair<int, int> 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<int> 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<<dis[n]<<endl;
return 0;
}

View File

View File

View File

View File

@ -0,0 +1,7 @@
## z:\Chao\src\Template\test\in.txt
2020/03/14 ÖÜÁù 11:41:28.68
Hello Easy C++ project!
-----------------------------------------------
Process exited after 200 ms with return value 0

View File

@ -7,6 +7,6 @@
### 关键词
### 易错点
*
### 工具箱
*