标签:练习 背景 sdi dig == turn color div print
Floyd
dijstra+堆优化
真是快的一批。。
1 #include <iostream> 2 #include <cstdio> 3 #define R register 4 #define INF 0x7fffffff 5 using namespace std; 6 int ans=INF; 7 int n,p,c,g[810][810],a[510]; 8 inline int ri(){ 9 char c=getchar();int x=0,w=1; 10 while(!isdigit(c)){if(c==‘-‘)w=-1;c=getchar();} 11 while( isdigit(c)){x=(x<<3)+(x<<1)+c-48;c=getchar();} 12 return x*w; 13 } 14 int main(){ 15 int num,x,y; 16 for(R int i=1;i<=800;++i) 17 for(R int j=1;j<=800;++j) 18 g[i][j]=1000000; 19 20 for(R int i=1;i<=800;++i) 21 g[i][i]=0; 22 23 n=ri(),p=ri(),c=ri(); 24 for(R int i=1;i<=n;++i) 25 num=ri(),a[num]++; 26 27 for(R int i=1;i<=c;++i) 28 x=ri(),y=ri(),num=ri(),g[x][y]=g[y][x]=num; 29 30 for(R int k=1;k<=p;++k) 31 for(R int i=1;i<=p;++i) 32 for(R int j=1;j<=p;++j) 33 if(g[i][k]+g[k][j]<g[i][j])g[i][j]=g[i][k]+g[k][j]; 34 for(R int i=1;i<=p;++i){ 35 int tot=0; 36 for(R int j=1;j<=p;++j)tot+=g[i][j]*a[j]; 37 ans=min(ans,tot); 38 } 39 printf("%d",ans); 40 return 0; 41 }
#include <iostream> #include <cstdio> #define R register #define INF 0x7fffffff using namespace std; int ans=INF; int n,p,c,g[810][810],a[510]; inline int ri(){ char c=getchar();int x=0,w=1; while(!isdigit(c)){if(c==‘-‘)w=-1;c=getchar();} while( isdigit(c)){x=(x<<3)+(x<<1)+c-48;c=getchar();} return x*w; } int main(){ int num,x,y; for(R int i=1;i<=800;++i) for(R int j=1;j<=800;++j) g[i][j]=1000000; for(R int i=1;i<=800;++i) g[i][i]=0; n=ri(),p=ri(),c=ri(); for(R int i=1;i<=n;++i) num=ri(),a[num]++; for(R int i=1;i<=c;++i) x=ri(),y=ri(),num=ri(),g[x][y]=g[y][x]=num; for(R int k=1;k<=p;++k) for(R int i=1;i<=p;++i) for(R int j=1;j<=p;++j) if(g[i][k]+g[k][j]<g[i][j])g[i][j]=g[i][k]+g[k][j]; for(R int i=1;i<=p;++i){ int tot=0; for(R int j=1;j<=p;++j)tot+=g[i][j]*a[j]; ans=min(ans,tot); } printf("%d",ans); return 0; }
标签:练习 背景 sdi dig == turn color div print
原文地址:https://www.cnblogs.com/flicker-five/p/10330351.html