标签:
三进制状压DP,预处理就可以了,不过数据好大好难调。。
没注意重边WA了一版。。不会生出图的数据,就干脆建了个完全图
1 //#include<bits/stdc++.h> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #include<iostream> 7 #define inc(i,l,r) for(int i=l;i<=r;i++) 8 #define dec(i,l,r) for(int i=l;i>=r;i--) 9 #define link(x) for(edge *j=h[x];j;j=j->next) 10 #define mem(a) memset(a,0,sizeof(a)) 11 #define inf 1e9 12 #define ll long long 13 #define succ(x) (1<<x) 14 #define NM 12 15 using namespace std; 16 int read(){ 17 int x=0,f=1;char ch=getchar(); 18 while(!isdigit(ch)){if(ch==‘-‘)f=-1;ch=getchar();} 19 while(isdigit(ch))x=x*10+ch-‘0‘,ch=getchar(); 20 return x*f; 21 } 22 int mul[20],_x,_y,n,m,d[60000][NM],a[NM][NM],v[NM],ans; 23 int main(){ 24 mul[0]=1; 25 inc(i,1,11)mul[i]=mul[i-1]*3; 26 while(scanf("%d%d",&n,&m)!=EOF){ 27 mem(a);mem(d); 28 inc(i,1,n) 29 inc(j,1,n)a[i][j]=inf; 30 inc(i,1,m){ 31 _x=read();_y=read(); 32 a[_y][_x]=a[_x][_y]=min(a[_x][_y],read()); 33 } 34 m=mul[n]-1; 35 inc(i,1,n) 36 inc(t,1,m)d[t][i]=inf; 37 inc(i,1,n)d[mul[i-1]][i]=0; 38 inc(t,1,m) 39 inc(i,1,n) 40 if(d[t][i]<inf){ 41 mem(v); 42 inc(j,1,n)v[j]=t%mul[j]/mul[j-1]; 43 inc(j,1,n) 44 if(v[j]!=2&&a[i][j]<inf) 45 d[t+mul[j-1]][j]=min(d[t+mul[j-1]][j],d[t][i]+a[i][j]); 46 } 47 // inc(t,1,m){ 48 // inc(i,1,n)printf("%d ",d[t][i]); 49 // printf("\n"); 50 // } 51 ans=inf; 52 inc(t,1,m){ 53 _x=1; 54 inc(i,1,n) 55 if(t%mul[i]/mul[i-1]==0)_x=0; 56 if(_x) 57 inc(i,1,n) 58 ans=min(ans,d[t][i]); 59 } 60 if(ans==inf)printf("-1\n");else printf("%d\n",ans); 61 } 62 return 0; 63 }
标签:
原文地址:http://www.cnblogs.com/onlyRP/p/5064566.html