This commit is contained in:
ljcjames 2022-08-15 16:53:11 +08:00
parent 5496c4f4a4
commit d34e7717dc
7 changed files with 194 additions and 0 deletions

View File

@ -0,0 +1,12 @@
# 题目
* 链接
### 题意
### 做法
### 关键词
### 易错点
### 工具箱
*

View File

@ -0,0 +1 @@
#

101
8.15/B-House Man/main.cpp Normal file
View File

@ -0,0 +1,101 @@
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int N=1e3+5,N3=N*3,inf=0x3f3f3f3f;
int to[N3],ne[N3],w[N3],h[N],cnt=0,s=N-4,e,dist[N],been[N];
bool in[N],wa=0;
int t,n,d;
struct Point
{
int h,id;
}a[N];
void add(int u,int v,int w1)
{
to[++cnt]=v;
ne[cnt]=h[u];
w[cnt]=w1;
h[u]=cnt;
}
void spfa()
{
queue<int>q;//queue没有自带函数清零
q.push(s);
while(!q.empty())
{
int u=q.front();
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;
been[v]++;//TLE 放错位置
if(been[v]>=n)
{
wa=1;
return;
}
// printf("%d(%d) %d(%d) %d\n",u,dist[u],v,dist[v],w1);
if(!in[v])
{
q.push(v);
in[v]=1;
}
}
}
}
}
bool cmp(Point a, Point b)
{
return a.h<b.h;
}
int main()
{
scanf("%d",&t);
for(int t1=1;t1<=t;t1++)
{
scanf("%d%d",&n,&d);
for(int i=0;i<=n;i++)
{
dist[i]=inf;
}
memset(h,0,sizeof(int)*(n+1));
memset(been,0,sizeof(int)*(n+1));
memset(in,0,sizeof(bool)*(n+1));
cnt=0;//WA罪魁祸首!
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i].h);//怎么总是你&
a[i].id=i;
// add(s,i,0);
}
for(int i=1;i<n;i++)
{
// add(i,i+1,d);
add(i+1,i,-1);
}
sort(a+1,a+n+1,cmp);
for(int i=1;i<n;i++)
{
int u=a[i].id,v=a[i+1].id;
if(u>v) swap(u,v);
add(u,v,d);
// add(v,u,-1);
}
s=a[1].id<a[n].id? a[1].id:a[n].id;
e=a[1].id>a[n].id? a[1].id:a[n].id;
dist[s]=0;
wa=0;//WA 忘了
spfa();
// if(dist[e]>=inf) dist[e]=-1;
if(wa) printf("Case %d: %d\n",t1,-1);
else printf("Case %d: %d\n",t1,dist[e]);
}
return 0;
}

View File

View File

@ -0,0 +1,3 @@
1
5 9
1 2 5 3 4

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

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

@ -0,0 +1,70 @@
//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;
}