A cool exam T110078. "One pass 3.2 practice 4" New Year Good topic description
Original title from: Cqoi 2005
Chongqing City has a (n\) station,\ (m\) two-way road connected to some of the stations. Each of the two stations is connected at most one road and can be reached from any station by one or more roads to other stations, but different routes may take different times. The amount of time spent on one path equals the sum of time required for all highways on the path.
Jia Jia's home at the station \ (1\), he has five relatives, respectively live in the station \ (a\),\ (b\),\ (c\),\ (d\),\ (e\). New Year, he needs to start from his home, visit each relative (order any), send them to the holiday blessing. How to walk, only need the least time?
Input format
The first line:\ (n\),\ (m\) is the number of stations and the number of highways.
The second line:\ (a\),\ (b\),\ (c\),\ (d\),\ (e\) is the station number for five relatives.
The following \ (m\) lines, three integers per line \ (x\),\ (y\),\ (t\), two station numbers and times for road connections.
Output format
Outputs only one row, containing an integer \ (t\), which is the minimum total time.
Example Sample input
6 6
2 3 4) 5 6
1 2 8
2 3 3
3 4 4
4 5 5
5 6 2
1 6 7
Sample output
21st
Data range and hints
For all data,\ (1≤n≤50000\),\ (1≤m≤10^5\),\ (1<a,b,c,d,e≤n\),\ (1≤x,y≤n\) ,\ (1≤t≤100\)
Positive solution did not want to come out, wrote \ (n^3\) violence, and then the hands of the base I hit 1, violent explosion 0 (after a change, \ (n^3 \) can be too 1000,loj too strong)
Positive solution is to find points 1 and five relative to the shortest point to all points, and then Dfs how to sort the best
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #define INF 0x7ffffffusing namespace Std;const int n=50005;struct node{int to,nxt,w;} E[n<<2];int head[n],tot=0,a[10];int dt[50];int dis[7][n],vis[n],vis2[n];int n,m,ans=inf;void Add (int x,int y, int z) {e[++tot]= (node) {y,head[x],z}; Head[x]=tot;} void Spfa (int s,int k) {queue<int>q; memset (vis2,0,sizeof (VIS2)); Q.push (s); Vis2[s]=1; dis[k][s]=0; while (!q.empty ()) {int U=q.front (); Q.pop (); vis2[u]=0; for (int i=head[u];i;i=e[i].nxt) {int v=e[i].to; if (DIS[K][V]>DIS[K][U]+E[I].W) {DIS[K][V]=DIS[K][U]+E[I].W; if (!vis2[v]) {vis2[v]=1; Q.push (v); }}}}}void dfs (int k) {if (k==6) {int s=0; for (int i=1;i<6;i++) s+=dis[dt[i]][a[dt[i+1]]; Ans=min (ans,s); Return } for (int i=2;i<=6;i++) {if (!vis[i]) {vis[i]=1; Dt[k+1]=i; DFS (K+1); vis[i]=0; }}}int Main () {scanf ("%d%d", &n,&m); for (int i=2;i<=6;i++) scanf ("%d", &a[i]); for (int i=1;i<=m;i++) {int a,b,c; scanf ("%d%d%d", &a,&b,&c); Add (A,B,C); Add (B,A,C); } memset (dis,0x3f,sizeof (dis)); A[1]=1; Dt[1]=1; for (int i=1;i<=6;i++) SPFA (a[i],i); DFS (1); printf ("%d", ans); return 0;}
T210178. "One-pass 5.5 cases 4" Travel problem Title description
Original title from: POI 2004
John is going to drive a car around a ring road. There are a total of n stations on the road, each station has a number of liters of petrol (some stations may be 0 oil), each liter of oil can allow the car to travel 1-kilometer. John had to go from one station to the other and go all the way in a clockwise (or counter-clockwise) direction and return to the starting point. In the beginning, the amount of oil in the car was zero, and John took all the oil from the station to a station (starting point is also the case), and no oil was allowed during the ride.
Mission: To determine whether the starting point for each station can be successfully traveled on a weekly basis.
Input format
The first line is an integer \ (n\), which indicates the number of stations on the Ring road;
Next \ (n\) line, two integers per line \ (p_i\),\ (d_i\), respectively, indicating the amount of oil and ( i\) of station ( i\) The distance from the station to the next station.
Output format
Output Total \ (n\) line, if starting from the station I\ , always in a clockwise (or counter-clockwise) direction, can successfully travel around a lap, then the i\ line output TAK, otherwise output NIE.
Example Sample input
5
3 1
1 2
5 2
0 1
5 4
Sample output
TAK
NIE
TAK
NIE
TAK
Data range and hints
For all data \ (3≤n≤10^6\),\ (0≤p_i≤2x10^9\),\ (0<d_i≤2x10^9\).
The question begins to look ... No, write violence,\ (n^2\), do not know why the examination of the brain residual (d_i\) think is point I to i-1 and i+1, the distance, continue to cool, do not know why there are 10 points
Positive Solution Monotone Queue
#include <cstdio> #include <iostream> #include <cstring>using namespace Std;const int N=2e6+1;int n,a[ N],head,tail,pq[n],last;long Long Q[n],p[n],qz[n];bool ans[n];void Work1 () {for (int i=1;i<=2*n-1;i++) qz[i]=q Z[i-1]+a[i]; head=1;tail=0; for (int i=1;i<=2*n-1;i++) {while (head<=tail&&q[tail]>=qz[i]) tail--; Q[++tail]=qz[i]; P[tail]=i; while (head<=tail&&i-p[head]>=n) head++; if (i>=n) {if (q[head]>=qz[i-n]) ans[i-n+1]=1; }} return; void Work2 () {qz[1]=pq[1]; for (int i=2;i<=n+1;i++) qz[i]=qz[i-1]+pq[n-i+2]; for (int i=n+2;i<=n*2-1;i++) qz[i]=qz[i-1]+pq[n*2-i+2]; head=1;tail=0; for (int i=1;i<=2*n-1;i++) {while (head<=tail&&q[tail]>=qz[i]) tail--; Q[++tail]=qz[i]; P[tail]=i; while (head<=tail&&i-p[head]>=n) head++; if (i>=n) { if (Q[head]>=qz[i-n]) {if (i==n) ans[1]=1; else ans[2*n-i+1]=1; }}} return;} int main () {scanf ("%d", &n); for (int i=1,u,v;i<=n;i++) {scanf ("%d%d", &u,&v); A[i+n]=a[i]=u-v; Pq[i+n]=pq[i]=u-last; Last=v; } pq[1]-=last; PQ[1+N]=PQ[1]; Work1 (); Work2 (); for (int i=1;i<=n;i++) {if (Ans[i]) puts ("TAK"); Else puts ("NIE"); } return 0;}
T310220. "One-pass 6.5 cases 2"fibonacci Nth topic description
Everyone knows Fibonacci series,\ (f_1=1\),\ (f_2=1\),\ (f_3=2\),\ (f_4=3\),??,\ (f_n=f_{ n?1}+f_{n?2}\).
Now the problem is simple, input \ (n\) and \ (m\), beg \ (fn?mod?m\).
Input format
Enter \ (n\),\ (m\).
Output format
Output \ (f_n?mod?m\).
Example Sample input
5 1000
Sample output
5
Data range and hints
For 100%100%100% data, \ (1≤n≤2x10^9\),\ (1≤m≤10^9+10\)
The only problem that didn't go wrong .....
Matrix Fast Power Board
#include<iostream>#include<cstdio>#include<cstring>#define int long longusing namespace std;int mod;struct node{ int a[2][2];}ss,ans;node mul(node &a,node &b){ node c; for(int i=0;i<2;++i) for(int j=0;j<2;++j) { c.a[i][j]=0; for(int k=0;k<2;++k) c.a[i][j]=(c.a[i][j]+(a.a[i][k]*b.a[k][j])%mod)%mod; } return c;}void fpow(int b){ while(b) { if(b&1)ans=mul(ans,ss); ss=mul(ss,ss); b>>=1; }}signed main(){ int n; scanf("%lld%lld",&n,&mod); ans.a[0][0]=ans.a[1][0]=1; ans.a[0][1]=ans.a[1][1]=0; ss.a[0][0]=ss.a[1][0]=ss.a[0][1]=1; ss.a[1][1]=0; fpow(n-1); printf("%lld",ans.a[0][0]); return 0;}
2018.10.10 Exams