NOIp2002神经网络 【拓扑排序】By cellur925

题目传送门

这道题目没有什么难的,是一道拓扑排序+递推的题目。我的思路是开始处理出拓扑序,然后因为数据范围很小怎么搞都可以,就邻接矩阵存图+暴力枚举。结果60分。

后来看题解发现,大家都是边拓扑边进行递推的,才发现自己这部分可能不对。另外的,就都是一些细节了==。

Code

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<queue>
 4 
 5 using namespace std;
 6 
 7 int n,m,tot,cnt;
 8 int c[500],head[500],cdu[500],rdu[500],seq[500];
 9 struct node{
10     int to,val,next;
11 }edge[20000];
12 
13 void add(int x,int y,int z)
14 {
15     edge[++tot].to=y;
16     edge[tot].next=head[x];
17     edge[tot].val=z;
18     head[x]=tot;
19 }
20 
21 void topo()
22 {
23     queue<int>q;
24     for(int i=1;i<=n;i++)
25         if(rdu[i]==0) q.push(i);
26     while(!q.empty())
27     {
28         int x=q.front();q.pop();
29         seq[++cnt]=x;
30         for(int i=head[x];i;i=edge[i].next)
31         {
32             int y=edge[i].to;
33             if(--rdu[y]==0) q.push(y);
34             if(c[x]>0) c[y]+=edge[i].val*c[x];
35         }
36     }
37 }
38 
39 int main()
40 {
41     scanf("%d%d",&n,&m);
42     for(int i=1;i<=n;i++)
43     {
44         int x=0;
45         scanf("%d%d",&c[i],&x);
46         if(c[i]==0) c[i]-=x;
47     }
48     for(int i=1;i<=m;i++)
49     {
50         int x=0,y=0,z=0;
51         scanf("%d%d%d",&x,&y,&z);
52         add(x,y,z);rdu[y]++;cdu[x]++;
53     }
54     topo();
55     bool flag=0;
56     for(int i=1;i<=n;i++)
57         if(cdu[i]==0&&c[i]>0) printf("%d %d\n",i,c[i]),flag=1;
58     if(!flag) printf("NULL");
59     return 0;
60 }
View Code

猜你喜欢

转载自www.cnblogs.com/nopartyfoucaodong/p/9691910.html