标签:技术分享 pac spfa one splay std for add char
1 #include<cstdio> 2 #include<iostream> 3 #include<cstdlib> 4 #include<algorithm> 5 #include<string> 6 #include<queue> 7 #include<ctime> 8 #define N 202020 9 #define ll long long 10 #define inf 1000000000 11 using namespace std; 12 int n,m,edgenum,head[N],vet[N],id=0,pri[N],pre[N],a[N],jie[N],st,ed; 13 char str[10]; 14 string b[300]; 15 int h[300][300],g[300][300],dis[N],f[N],num[N],inq[N],happy[N]; 16 queue<int> Q; 17 void add(int u,int v,int w){ 18 edgenum++;vet[edgenum]=v;jie[edgenum]=head[u];head[u]=edgenum;pri[edgenum]=w; 19 } 20 int change(){ 21 int ans=0; 22 for(int i=0;i<3;i++){ 23 int x=str[i]-‘A‘+1; 24 ans=ans*27+x; 25 } 26 return ans; 27 } 28 void go(int ee){ 29 if(pre[ee]==0){ 30 cout<<b[ee]; 31 return; 32 } 33 go(pre[ee]); 34 cout<<"->"<<b[ee]; 35 } 36 int main() 37 { 38 scanf("%d%d%s",&n,&m,str); 39 st=change(); 40 for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(i!=j)h[i][j]=inf;else h[i][j]=0; 41 id++;a[st]=id;st=id;b[id]=str; 42 for(int i=1;i<n;i++){ 43 scanf("%s",str);int x=change(); 44 if(a[x]==0)id++,a[x]=id,b[id]=str; 45 scanf("%d",&happy[a[x]]); 46 if(x==13540)ed=a[x]; 47 } 48 for(int i=1;i<=m;i++){ 49 scanf("%s",str); 50 int u=change(); 51 scanf("%s",str); 52 int v=change(); 53 int x;scanf("%d",&x);add(a[u],a[v],x);add(a[v],a[u],x); 54 if(x<h[a[u]][a[v]]){ 55 h[a[u]][a[v]]=x;h[a[v]][a[u]]=x; 56 g[a[u]][a[v]]=1;g[a[v]][a[u]]=1; 57 }else if(x==h[a[u]][a[v]]){ 58 g[a[u]][a[v]]++; 59 g[a[v]][a[u]]++; 60 } 61 } 62 for(int k=1;k<=n;k++){ 63 for(int i=1;i<=n;i++){ 64 for(int j=1;j<=n;j++){ 65 if(h[i][j]==h[i][k]+h[k][j]) 66 g[i][j]+=g[i][k]*g[k][j];else if(h[i][j]>h[i][k]+h[k][j]){ 67 h[i][j]=h[i][k]+h[k][j]; 68 g[i][j]=g[i][k]*g[k][j]; 69 } 70 } 71 } 72 } 73 Q.push(st); 74 for(int i=0;i<=n;i++){ 75 dis[i]=inf;f[i]=0;num[i]=inf; 76 } 77 dis[st]=0;f[st]=0;num[st]=0; 78 while(!Q.empty()){ 79 int u=Q.front();Q.pop(); 80 inq[u]=0;int e=head[u]; 81 while(e>0){ 82 int v=vet[e]; 83 if((dis[u]+pri[e]<dis[v]) || (dis[u]+pri[e]==dis[v] && f[u]+happy[v]>f[v]) || 84 ( dis[u]+pri[e]==dis[v] && f[u]+happy[v]==f[v] && num[u]+1<num[v])) { 85 dis[v]=dis[u]+pri[e];f[v]=f[u]+happy[v]; 86 num[v]=num[u]+1;pre[v]=u; 87 if(inq[v]==0){ 88 inq[v]=1;Q.push(v); 89 } 90 } 91 e=jie[e]; 92 } 93 94 } 95 printf("%d %d %d %d\n",g[st][ed],h[st][ed],f[ed],f[ed]/num[ed]); 96 go(ed); 97 return 0; 98 }
(Floyed + SPFA,略带记录路径)
标签:技术分享 pac spfa one splay std for add char
原文地址:https://www.cnblogs.com/wxxlouisa/p/10082347.html