Solution to the problem of roads roads in Luogu sp338

Keywords: PHP

thinking

The adjacency table of dfs is composed of two parts: header node and table node. The header node stores each vertex of the graph, and the table node stores the adjacent vertex (that is, the edge of the graph) of the corresponding vertex of the header node with a one-way linked list. In the directed graph, it indicates that the header node points to other nodes (a - > b). In the undirected graph, it indicates all nodes (a-b) adjacent to the header node.

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 const int N = 1010;
 6 const int INF = 1e6;
 7 
 8 struct node {
 9     int s,e,len,cost;
10     int nxt;
11 } cc[10*N];
12 
13 int n,m,num,head[N],vis[N];
14 int ans;
15 //Insert an edge 
16 void cr(int a,int b,int c,int d) {
17     cc[num].s=a;
18     cc[num].e=b;
19     cc[num].len=c;
20     cc[num].cost=d;
21     cc[num].nxt=head[a];//Make the inserted element point to the next 
22     head[a]=num++;//Make the previous point to the inserted 
23 }
24 //x Represents the current starting point. y Indicates the length of the walk path. z Represents the current total charge 
25 void dfs(int x,int y,int z) {
26     if(y>ans)return;
27     if(x==n && z>=0 && y<ans) ans=y;
28     for(int i=head[x]; i!=-1; i=cc[i].nxt) {
29         int u=cc[i].e;
30         if(!vis[u] && z >= cc[i].cost) {
31             vis[u]=1;
32             dfs(u,y+cc[i].len,z-cc[i].cost);
33             vis[u]=0;
34         }
35     }
36 }
37 
38 int main() {
39     int i,a,b,c,d,k;
40     int yyy;
41     cin>>yyy;
42     while(yyy--) {
43         cin>>k>>n>>m;
44         num=0;
45         memset(head,-1,sizeof(head));
46         for(i=0; i<m; i++) {
47             scanf("%d%d%d%d",&a,&b,&c,&d);
48             cr(a,b,c,d);
49         }
50         ans=INF;
51         memset(vis,0,sizeof(vis));
52         dfs(1,0,k);
53 //      printf(ans<INF?"%d\n":"-1\n",ans);
54         if(ans<INF)cout<<ans<<endl;
55         else cout<<-1<<endl;
56     }
57 
58     return 0;
59 }
I don't know what it means.

Posted by Yaak on Thu, 17 Oct 2019 11:11:03 -0700