LUOGU P1342 请柬

建正、反向图,分别跑spfa/dij,然后统计两次所有点的距离的和。

一开始想偷懒,建一个图,然后通过边的编号的奇偶性判断它是正/反向图,可是聪明反被聪明误,tle了

QAQ

偷懒不成反倒吃亏。

 1 #include<bits/stdc++.h>
 2 #pragma GCC optimize(3)
 3 namespace ZDY{
 4     #define res register
 5     #define ri res int
 6     #define ll long long
 7     #define db double
 8     #define il inline
 9     #define MB template <class T>
10     #define Fur(i,x,y) for(ri i=x;i<=y;i++)
11     #define fur(i,x,y) for(i=x;i<=y;i++)
12     #define Fdr(i,x,y) for(ri i=x;i>=y;i--)
13     #define in2(x,y) in(x),in(y)
14     #define in3(x,y,z) in2(x,y),in(z)
15     #define in4(a,b,c,d) in2(a,b);in2(c,d)
16     #define outn(x) out(x),pc('
')
17     #define clr(x,y) memset(x,y,sizeof(x))
18     #define cpy(x,y) memcpy(x,y,sizeof(x))
19     #define fl(i,x) for(ri i=head[x],to;to=e[i].to,i;i=e[i].nxt)
20     #define inf 2147483647
21     #define fin(s) freopen(s".in","r",stdin)
22     #define fout(s) freopen(s".out","w",stdin)
23     #define gt io.gc()
24     MB il T ABS(T x){return x>0?x:-x;}
25     MB il T MAX(T x,T y){return x>y?x:y;}
26     MB il T MIN(T x,T y){return x<y?x:y;}
27     MB il T GCD(T x,T y){return y?GCD(y,x%y):x;}
28     MB il void SWAP(T x,T y){T t=x;y=t;x=y;}
29 }using namespace ZDY;using namespace std;
30 
31 class IO{
32    #define fok (ch!=EOF)
33    #define sep (ch==' '||ch=='
'||ch=='	')
34    #define dsep !isdigit(ch)
35    #define neq(a,b) ((a)-(b)>1e-6)
36    char rbuf[1000002],wbuf[1000002],b,*p1,*p2;
37    int rs,ws,S;
38    public:
39        IO():p1(rbuf),p2(wbuf),S(1000000),rs(1000000),ws(-1),b(1){}
40        ~IO(){fwrite(wbuf,1,ws+1,stdout);}
41        il char gc(){return rs==S&&(p1=rbuf,rs=-1,(S=fread(rbuf,1,S+1,stdin)-1)==-1)?(b=0,EOF):(++rs,*p1++);}
42        il void pc(int x){ws==1000000&&(p2=wbuf,ws=-1,fwrite(wbuf,1,1000001,stdout)),++ws,*p2++=x;}
43        il void puts(const char str[]){fwrite(wbuf,1,ws+1,stdout)?(ws=-1):0,fwrite(str,1,strlen(str),stdout);}
44        il void gl(string& s){for(res char ch;(ch=gc())!='
'&&fok;)s+=ch;}
45        il IO& operator>>(int& x){x=0;res char f=0,ch=gc();while(dsep&&fok)f|=(ch=='-'),ch=gc();while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=gc();return x=f?-x:x,*this;}
46        il IO& operator>>(ll& x){x=0;res char f=0,ch=gc();while(dsep&&fok)f|=(ch=='-'),ch=gc();while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=gc();return x=f?-x:x,*this;}
47        il IO& operator>>(char& ch){return ch=gc(),*this;}
48        il IO& operator>>(string& s){res char ch=gc();while(sep&&fok)ch=gc();while(!sep&&fok)s+=ch,ch=gc();return *this;}
49        il IO& operator>>(double& x){x=0;res char f=0,ch=gc();double d=0.1;while(dsep&&fok)f|=(ch=='-'),ch=gc();while(isdigit(ch))x=x*10+(ch^48),ch=gc();if(ch=='.')while(isdigit(ch=gc()))x+=d*(ch^48),d*=0.1;return x=f?-x:x,*this;}
50        il IO& operator<<(int x){res char ch[10],i=-1;!x?(pc('0'),0):0,x<0?(pc('-'),x=-x):0;while(x)ch[++i]=x%10+48,x/=10;while(~i)pc(ch[i]),--i;return *this;}
51        il IO& operator<<(ll x){res char ch[20],i=-1;!x?(pc('0'),0):0,x<0?(pc('-'),x=-x):0;while(x)ch[++i]=x%10+48,x/=10;while(~i)pc(ch[i]),--i;return *this;}
52        il IO& operator<<(char ch){return pc(ch),*this;}
53        il IO& operator<<(char str[]){return puts(str),*this;}
54        il IO& operator<<(double x){int y=int(x);*this<<y;x-=y;while(neq(x,int(x)))x*=10;x?*this<<'.'<<int(x):0;return *this;}
55        il operator bool(){return b;}
56 }io;
57 
58 #define N 1000001
59 #define Fl(i,x) for(ri i=Head[x],to;to=E[i].to,i;i=E[i].nxt)
60 int n,m,head[N],Head[N],cnt=0,Cnt=0;
61 ll d[N],ans=0;
62 bool v[N];
63 struct cmp{bool operator()(int a,int b){return d[a]>d[b];}};
64 struct edge{int to,w,nxt;}e[N],E[N];
65 il void add(int x,int y,int w){e[++cnt].to=y;e[cnt].w=w;e[cnt].nxt=head[x];head[x]=cnt;}
66 il void ADD(int x,int y,int w){E[++Cnt].to=y;E[Cnt].w=w;E[Cnt].nxt=Head[x];Head[x]=Cnt;}
67 priority_queue<int,vector<int>,cmp>q;
68 il void spfa(int p){
69     Fur(i,2,n)d[i]=1e15;
70     d[1]=0;q.push(1);
71     int x;
72     while(!q.empty()){
73         v[x=q.top()]=0;q.pop();
74         if(p){
75             fl(i,x)
76             if(d[x]+e[i].w<d[to]){
77                 d[to]=d[x]+e[i].w;
78                 if(!v[to])q.push(to),v[to]=1;
79             }
80         }
81         else{
82             Fl(i,x)
83             if(d[x]+E[i].w<d[to]){
84                 d[to]=d[x]+E[i].w;
85                 if(!v[to])q.push(to),v[to]=1;
86             }
87         } 
88     }
89 }
90 int main(){
91     io>>n>>m;
92     int x,y,w;
93     Fur(i,1,m)io>>x>>y>>w,add(x,y,w),ADD(y,x,w);
94     Fur(i,0,1){spfa(i);Fur(j,1,n)ans+=d[j];}
95     io<<ans<<'
';
96 }
献上复活的spfa
原文地址:https://www.cnblogs.com/mimiorz/p/9736201.html