The first scene of HDU multi school in 2019
Keywords:
Python
PHP
1004.Vacation
Delivery: http://acm.hdu.edu.cn/showproblem.php?pid=6581
There are $n $cars in front of you at an intersection. Give the length of each car, the distance from the intersection, and the maximum speed of each car. Ask these $n+1 $cars to pass by, and ask how long it took your car to cross the intersection.
Data range: $1 < = n < = 10 ^ 5,1 < = s \, V \, l \ = 10 ^ 9 $.
Analysis:
1 #include<bits/stdc++.h>
2 #define ll long long
3 using namespace std;
4 const int maxn=1e5+7;
5 struct node{int l,s,v;ll len;} p[maxn];
6 int main()
7 {
8 int n;
9 while (~scanf("%d",&n))
10 {
11 for (int i=0;i<=n;i++) p[i].l=p[i].s=p[i].v=p[i].len=0;
12 for (int i=0;i<=n;i++) scanf("%d",&p[i].l);
13 for (int i=0;i<=n;i++) scanf("%d",&p[i].s);
14 for (int i=0;i<=n;i++) scanf("%d",&p[i].v);
15 for (int i=1;i<=n;i++) p[i].len+=p[i-1].len+1ll*p[i].l;
16 for (int i=1;i<=n;i++) p[i].len+=1ll*p[i].s;
17 p[0].len=p[0].s;
18 double ans=0.0;
19 for (int i=0;i<=n;i++) ans=max(ans,1.0*p[i].len/(1.0*p[i].v));
20 printf("%.10f\n",ans);
21 }
22 return 0;
23 }
1004
1005.Path
Delivery: http://acm.hdu.edu.cn/showproblem.php?pid=6582
Problem meaning: there are $n $points, $m $roads, and some roads are required to be cut off, so that the road from $1-n $costs more than the shortest path. The cost of cutting each road is the length of each road.
Data range: $1 < = n, m < = 10 ^ 5, 1 < = C < = 10 ^ 9 $.
Analysis: the requirement of the problem is to remove some paths so that the shortest path does not hold. Run the shortest path, select all the shortest edges, and then run the shortest cut with these edges.
1 #include<cstdio>
2 #include<iostream>
3 #include<cstring>
4 #include<algorithm>
5 #include<queue>
6 #define en '\n'
7 #define low(x) (x)&(-x)
8 #define m(a,b) memset(a,b,sizeof a)
9 using namespace std;
10 typedef long long ll;
11 const int N=2e4+10,M=N;
12 const ll INF=1e18;
13 struct Edge{int to;ll len;int nex;}e[M],edge[M<<1];
14 struct node{int x,y;ll c;}p[M];
15 int head[N],tot,head2[N],ttt,n,m;
16 void add(int from,int to,ll len)
17 {
18 edge[++tot]=(Edge){to,len,head[from]};head[from]=tot;
19 edge[++tot]=(Edge){from,0,head[to]};head[to]=tot;
20 }
21 void add_edge(int from,int to,ll len){
22 e[++ttt]=(Edge){to,len,head2[from]};head2[from]=ttt;
23 }
24 priority_queue<pair<ll,int>>Q;
25 ll d[N],da[N],db[N];
26 void dijkstra(int s)
27 {
28 for (int i=0;i<=n;i++) d[i]=INF;
29 d[s]=0;Q.push(make_pair(0,s));
30 while(!Q.empty())
31 {
32 int x=Q.top().second;Q.pop();
33 for(int i=head2[x];i;i=e[i].nex)
34 {
35 int y=e[i].to;ll z=e[i].len;
36 if(d[y]>d[x]+z)
37 {
38 d[y]=d[x]+z;
39 Q.push(make_pair(-d[y],y));
40 }
41 }
42 }
43 }
44 int s,t;
45 queue<int>q;
46 bool bfs()
47 {
48 for (int i=0;i<=n;i++) d[i]=0;
49 while(!q.empty())q.pop();
50 q.push(s);d[s]=1;
51 while(!q.empty())
52 {
53 int x=q.front();q.pop();
54 for(int i=head[x];i;i=edge[i].nex)
55 {
56 int y=edge[i].to;ll l=edge[i].len;
57 if(!d[y]&&l)
58 {
59 q.push(y),d[y]=d[x]+1;
60 if(y==t) return 1;
61 }
62 }
63 }
64 return 0;
65 }
66 ll dinic(int x,ll flow)
67 {
68 if(x==t) return flow;
69 ll res=flow,k;
70 for(int i=head[x];i&&res;i=edge[i].nex)
71 {
72 int y=edge[i].to;ll l=edge[i].len;
73 if(l&&d[y]==d[x]+1)
74 {
75 k=dinic(y,min(res,l));
76 if(!k){d[y]=0;continue;}
77 edge[i].len-=k,edge[i^1].len+=k,res-=k;
78 }
79 }
80 return flow-res;
81 }
82 int main()
83 {
84 int T;scanf("%d",&T);
85 while(T--)
86 {
87 scanf("%d%d",&n,&m);
88 for (int i=0;i<=n;i++) head[i]=0,head2[i]=0;
89 ttt=0;tot=1;
90 for(int i=1;i<=m;++i)
91 {
92 scanf("%d%d%lld",&p[i].x,&p[i].y,&p[i].c);
93 add_edge(p[i].x,p[i].y,p[i].c);
94 }
95 dijkstra(1);
96 if(d[n]==INF) {puts("0");continue;}
97 for (int i=0;i<=n;i++) da[i]=d[i],head2[i]=0;
98 ttt=0;
99 for(int i=1;i<=m;++i) add_edge(p[i].y,p[i].x,p[i].c);
100 dijkstra(n);
101 for (int i=1;i<=n;i++) db[i]=d[i];
102 for(int i=1;i<=m;++i)
103 {
104 if(da[p[i].x]+db[p[i].y]+p[i].c==da[n])
105 add(p[i].x,p[i].y,p[i].c);
106 }
107 for (int i=0;i<=n;i++) d[i]=0;
108 s=1,t=n;
109 ll maxflow=0;
110 while(bfs()) maxflow+=dinic(s,INF);
111 printf("%lld\n",maxflow);
112 }
113 }
1005