bzoj2337: [HNOI2011]XOR和路径
第二个样例是
3 3
1 2 4
1 3 5
2 3 6
题解:http://blog.csdn.net/thy_asdf/article/details/47355475
code:
1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #define maxn 105 7 #define maxm 10005 8 using namespace std; 9 char ch; 10 int n,m,u[maxm],v[maxm],val[maxm],deg[maxn]; 11 double a[maxn][maxn],b[maxn],x[maxn],ans; 12 bool ok; 13 void read(int &x){ 14 for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=1; 15 for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar()); 16 if (ok) x=-x; 17 } 18 void gauss(){ 19 int i,j,k; 20 for (i=1,k=1;i<=n;i++){ 21 for (j=k;j<=n&&!a[j][i];j++); 22 if (j<=n){ 23 if (j!=k){ 24 for (int p=i;p<=n;p++) swap(a[j][p],a[k][p]); 25 swap(b[j],b[k]); 26 } 27 for (j++;j<=n;j++) if (a[j][i]){ 28 double t=a[j][i]/a[k][i]; 29 for (int p=i;p<=n;p++) a[j][p]-=a[k][p]*t; 30 b[j]-=b[k]*t; 31 } 32 k++; 33 } 34 else{ 35 x[i]=0; 36 for (j=k-1;j;j--) a[j][i]=0; 37 } 38 } 39 for (k--,i=n;i;i--) 40 if (a[k][i]){ 41 x[i]=b[k]/a[k][i]; 42 for (j=k-1;j;j--) if (a[j][i]) b[j]-=a[j][i]*x[i],a[j][i]=0; 43 k--; 44 } 45 } 46 int main(){ 47 read(n),read(m); 48 for (int i=1;i<=m;i++){ 49 read(u[i]),read(v[i]),read(val[i]); 50 deg[u[i]]++;if (u[i]!=v[i]) deg[v[i]]++; 51 } 52 for (int k=0;k<=30;k++){ 53 memset(a,0,sizeof(a)); 54 memset(b,0,sizeof(b)); 55 memset(x,0,sizeof(x)); 56 for (int i=1;i<n;i++) a[i][i]=-1; 57 for (int i=1;i<=m;i++){ 58 if (u[i]!=n){ 59 if (val[i]&(1<<k)) a[u[i]][v[i]]-=1.0/deg[u[i]],b[u[i]]-=1.0/deg[u[i]]; 60 else a[u[i]][v[i]]+=1.0/deg[u[i]]; 61 } 62 if (v[i]!=n&&u[i]!=v[i]){ 63 if (val[i]&(1<<k)) a[v[i]][u[i]]-=1.0/deg[v[i]],b[v[i]]-=1.0/deg[v[i]]; 64 else a[v[i]][u[i]]+=1.0/deg[v[i]]; 65 } 66 } 67 a[n][n]=1,gauss(),ans+=x[1]*(1<<k); 68 } 69 printf("%.3lf\n",ans); 70 return 0; 71 }