Megan super electromagnetic gun transmission (Luogu)
Megan super electromagnetic gun transmission (vijos)
(that's right. It's sister Pao's super electromagnetic gun. Is it full of acid?)
Show your wife!
Knowledge points
graph theory
Adjacency matrix, bfs
Fool around with
Basic thinking
Adjacency matrix map
bfs
output
explicate
Adjacency matrix map
This is what I did when I read in. It seems that I have nothing to say.
bfs
This is a key step.
First, we scan out the input points and put them in the queue. (the input point is the one where the read-in c is not equal to 0)
Note: the input point does not need to have a status greater than the threshold
So when you throw it in, just set the threshold to zero by the way.
Let's cycle bfs.
Every time we traverse to a point, we first subtract the u value from its C value, because our c value is only passed from the previous point, and we also need to subtract the threshold value. And that's why we set the threshold to 0 when we throw in the input point
If the processed c is less than 0, continue to the next point.
Otherwise, we will find the points he points to in turn, then deal with them and throw them into the queue.
That's ok!
output
Why do you want to talk about the output alone?
Because the end point is involved, how should we judge the end point?
If you look at my code, you will find that there is an array of ansv []
His meaning is, if he passes the nerve impulse to the next point, set to 1
In this case, 0 is the end point.
Comment on difficulty
Water problem! I can only say that.
However, it is a very classic question.
Code
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int n,m;
int c[105],u[105];
int mapp[105][105];
int vis[105];
int ansv[105];
queue<int> q;
void work(){
memset(vis,0,sizeof(vis));
memset(ansv,0,sizeof(ansv));
for(int i=1;i<=n;i++){
if(c[i]!=0){
q.push(i);
vis[i]=1;
u[i]=0;
}
}
while(!q.empty()){
int cur=q.front();
q.pop();
c[cur]-=u[cur];
if(c[cur]>0){
for(int i=1;i<=n;i++){
if(mapp[cur][i]!=0x3f3f3f3f){
ansv[cur]=1;
c[i]+=c[cur]*mapp[cur][i];
if(vis[i]==0){
q.push(i);
vis[i]=1;
}
}
}
}
}
}
int main(){
memset(mapp,0x3f,sizeof(mapp));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d%d",&c[i],&u[i]);
}
for(int i=0;i<m;i++){
int s,t,w;
scanf("%d%d%d",&s,&t,&w);
mapp[s][t]=w;
}
work();
int ok=0;
for(int i=1;i<=n;i++){
if(ansv[i]==0&&c[i]>0){
ok=1;
printf("%d %d\n",i,c[i]);
}
}
if(ok==0){
printf("NULL");
}
return 0;
}