poj 2404 Jogging Trails

呵呵呵,什么什么传说中(看了题解才知道)的中国什么(邮路)问题,%%http://hzwer.com/5998.html

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 inline int ra()
 4 {
 5     int x=0,f=1; char ch=getchar();
 6     while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
 7     while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
 8     return x*f;
 9 }
10 int n,m,ed,tot,top;
11 int d[20],q[20];
12 int f[32768],dis[20][20];
13 int bin[20];
14 void floyd()
15 {
16     for (int k=1; k<=n; k++)
17             for (int i=1; i<=n; i++)
18                     for (int j=1; j<=n; j++) 
19                         dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); 
20 }
21 void dp()
22 {
23     memset(f,127/3,sizeof(f));
24     f[0]=0;
25     for (int i=0; i<ed; i++)
26     {
27         int x=1;
28         while (bin[x-1]&i) x++;
29         for (int y=x+1; y<=top; y++)
30                 if ((i&bin[y-1])==0)
31                     f[i|bin[y-1]|bin[x-1]]=min(f[i|bin[y-1]|bin[x-1]],f[i]+dis[q[x]][q[y]]);
32     }
33 }
34 int main(int argc, char const *argv[])
35 {
36     bin[0]=1; for (int i=1; i<=20; i++) bin[i]=bin[i-1]<<1;
37     while (1)
38     {
39         top=tot=0;
40         memset(dis,127/3,sizeof(dis));
41         memset(d,0,sizeof(d));
42         n=ra(); if (!n) break; m=ra();
43         for (int i=1; i<=m; i++)
44         {
45             int x=ra(),y=ra(),v=ra();
46             dis[x][y]=dis[y][x]=min(dis[x][y],v);
47             d[x]++; d[y]++; tot+=v;
48         }
49         floyd();
50         for (int i=1; i<=n; i++)
51             if (d[i]&1) q[++top]=i;
52         ed=bin[top]-1;
53         dp();
54         cout<<f[ed]+tot<<endl;
55     }
56     return 0;
57 }

 

posted @ 2017-03-06 21:21  ws_ccd  阅读(144)  评论(0编辑  收藏  举报