因为时间问题,不提供思路。
1.踩点上课
#include<iostream>//贪心+BFS
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define INF 100000000000000LL
ll read()
{
ll x=0,f=1;
char ch=getchar();
while(ch<'0' || ch>'9'){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9')
{
x=(x<<3LL)+(x<<1LL)+(ll)(ch^48);
ch=getchar();
}
return x*f;
}
struct Node{
int x;
int y;
ll weight;
Node(int X,int Y,ll w):x(X),y(Y),weight(w){}
Node(){}
};
ll map[2005][2005];
queue<Node> q;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool mark[2005][2005];
ll ans=INF;
ll BFS(int s_x,int s_y,int e_x,int e_y,ll w)
{
int n=max(s_x,e_x);
int m=max(e_y,s_y);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
mark[i][j]=0;
q.push(Node(s_x,s_y,0));
mark[s_x][s_y]=1;
ll Min=INF;
while(!q.empty())
{
Node cur=q.front();
q.pop();
if(cur.x==e_x && cur.y==e_y)
{
ans=min(ans,cur.weight);
}
if(map[cur.x][cur.y]>0)
{
Min=min(cur.weight+map[cur.x][cur.y],Min);
}
for(int i=0;i<4;i++)
{
int x=cur.x+dir[i][0];
int y=cur.y+dir[i][1];
if(x<=n && x>=1 && y<=m && y>=1 && map[x][y]!=-1 && mark[x][y]!=1)
{
q.push(Node(x,y,cur.weight+w));
mark[x][y]=1;
}
}
}
return Min;
}
int main()
{
int n,m,w;
cin>>n>>m;
w=read();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
ll x=read();
map[i][j]=x;
}
for(int i=0;i<=n;i++)
{
map[i][0]=-1;
}
for(int j=0;j<=m;j++)
{
map[0][j]=-1;
}
ans=min(ans,BFS(1,1,n,m,w)+BFS(n,m,1,1,w));
if(ans!=INF)
{
cout<<ans<<'\n';
}
else
{
cout<<-1<<'\n';
}
return 0;
}
2.有效的BFS
#include<iostream>
#include<vector>
#include<queue>
#include<unordered_map>
using namespace std;
int read()
{
int x=0;
char ch=getchar();
while(ch<'0' || ch>'9')ch=getchar();
while(ch>='0' && ch<='9')
{
x=(x<<3)+(x<<1)+(ch^48);
ch=getchar();
}
return x;
}
vector<int> G[200005];
bool mark[200005];
vector<int> a;
queue<int> q;
unordered_map<int,bool> mp;
int num=1;
int size=1;
int s=0,e=1;
bool BFS()
{
num=0;
mark[1]=1;
s=size;
while(!q.empty())
{
int cur=q.front();
q.pop();
int m=0;
for(auto it=G[cur].begin();it!=G[cur].end();it++)
{
if(mark[*it]!=1)
{
mark[*it]=1;
mp[*it]=1;
num++;
m++;
}
}
while(m--)
{
if(mp[a[size]]!=1)
return false;
mp[a[size]]=0;
size++;
}
}
e=size;
return true;
}
int main()
{
int n;
n=read();
for(int i=1;i<n;i++)
{
int x,y;
x=read();
y=read();
G[x].push_back(y);
G[y].push_back(x);
}
for(int i=0;i<n;i++)
{
int x=read();
a.push_back(x);
}
while(n)
{
for(int i=s;i<e;i++)
{
q.push(a[i]);
}
n-=num;
if(!BFS())
{
cout<<"No\n";
return 0;
}
}
cout<<"Yes\n";
return 0;
}
3.猫与餐厅的故事
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int read()
{
int x=0;
char ch=getchar();
while(ch<'0' || ch>'9')ch=getchar();
while(ch>='0' && ch<='9')
{
x=(x<<3)+(x<<1)+(ch^48);
ch=getchar();
}
return x;
}
int cnt[200005];
vector<int> G[200005];
int ans=0;
queue<int> q;
bool mark[200005];
void BFS(int m)
{
q.push(1);
mark[1]=1;
while(!q.empty())
{
int cur=q.front();
q.pop();
for(auto it=G[cur].begin();it!=G[cur].end();it++)
{
if(mark[*it]!=1)
{
mark[*it]=1;
if(cnt[*it]==1)
cnt[*it]+=cnt[cur];
if(cnt[*it]<=m)
q.push(*it);
}
}
if(G[cur].size()==1)
{
if(cnt[cur]<=m)
ans++;
}
}
}
int main()
{
int n,m;
n=read();
m=read();
for(int i=1;i<=n;i++)
{
int x=read();
cnt[i]+=x;
}
for(int i=1;i<n;i++)
{
int x=read();
int y=read();
G[x].push_back(y);
G[y].push_back(x);
}
BFS(m);
cout<<ans;
return 0;
}
4.良心树
#include<iostream>
using namespace std;
int read()
{
int x=0,f=1;;
char ch=getchar();
while(ch<'0' || ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9')
{
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
int num[100005];
int son[100005];
int main()
{
int n,root;
n=read();
for(int i=1;i<=n;i++)
{
int x,y;
x=read();
y=read();
if(x==-1)
{
x=0;
root=i;
}
num[x]+=y;
num[i]+=y;
son[x]++;
}
bool flag=0;
for(int i=1;i<=n;i++)
{
if(i!=root && num[i]==son[i]+1)
{
cout<<i<<' ';
flag=1;
}
}
if(!flag)
cout<<-1;
return 0;
}
5.最优灌溉
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int read()
{
int x=0;
char ch=getchar();
while(ch<'0' || ch>'9')ch=getchar();
while(ch>='0' && ch<='9')
{
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x;
}
struct vertex{
int v;
int w;
vertex(int V,int W):v(V),w(W){}
vertex(){}
bool operator >(const vertex& a)const
{
return this->w>a.w;
}
};
vector<vertex> G[1005];
bool mark[1005];
int ans=0;
priority_queue<vertex,vector<vertex>,greater<vertex> > q;
void MST()
{
q.push(vertex(1,0));
while(!q.empty())
{
vertex cur=q.top();
q.pop();
if(mark[cur.v]==1)
continue;
mark[cur.v]=1;
ans+=cur.w;
for(auto it=G[cur.v].begin();it!=G[cur.v].end();it++)
{
if(mark[it->v]!=1)
{
q.push(vertex(it->v,it->w));
}
}
}
return ;
}
int main()
{
int n,m;
n=read();
m=read();
for(int i=0;i<m;i++)
{
int x,y,w;
x=read();
y=read();
w=read();
G[x].push_back(vertex(y,w));
G[y].push_back(vertex(x,w));
}
MST();
cout<<ans;
return 0;
}