BZOJ1579: [Usaco2009 Feb]Revamping Trails 道路升级

1579: [Usaco2009 Feb]Revamping Trails 道路升级

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 1251  Solved: 337
[Submit][Status]

Description

每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M(1<=M<=50,000)条双向泥土道路,编号为1..M. 道路i连接牛棚P1_i和P2_i (1 <= P1_i <= N; 1 <= P2_i<= N). John需要T_i (1 <= T_i <= 1,000,000)时间单位用道路i从P1_i走到P2_i或者从P2_i 走到P1_i 他想更新一些路经来减少每天花在路上的时间.具体地说,他想更新K (1 <= K <= 20)条路经,将它们所须时间减为0.帮助FJ选择哪些路经需要更新使得从1到N的时间尽量少.

Input

* 第一行: 三个空格分开的数: N, M, 和 K * 第2..M+1行: 第i+1行有三个空格分开的数:P1_i, P2_i, 和 T_i

Output

* 第一行: 更新最多K条路经后的最短路经长度.

Sample Input

4 4 1
1 2 10
2 4 10
1 3 1
3 4 100

Sample Output

1

HINT

K是1; 更新道路3->4使得从3到4的时间由100减少到0. 最新最短路经是1->3->4,总用时为1单位. N<=10000

Source

题解:
没想到竟是如此暴力。。。直接把k加进状态里即可。。。我的思维还是不够暴力。。。
代码:
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #define inf 1000000000
12 #define maxn 10000+100
13 #define maxm 50000+100
14 #define eps 1e-10
15 #define ll long long
16 #define pa pair<int,int>
17 using namespace std;
18 inline int read()
19 {
20     int x=0,f=1;char ch=getchar();
21     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
22     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
23     return x*f;
24 }
25 int n,m,k,tot;
26 int d[maxn][25],head[maxn];
27 bool v[maxn][25];
28 struct edge{int go,next,w;}e[2*maxm];
29 void ins(int x,int y,int z)
30 {
31     e[++tot].go=y;e[tot].w=z;e[tot].next=head[x];head[x]=tot;
32 }
33 void insert(int x,int y,int z)
34 {
35     ins(x,y,z);ins(y,x,z);
36 }
37 void dijkstra()
38 {
39     priority_queue<pa,vector<pa>,greater<pa> >q;
40     for(int i=1;i<=n;i++)
41      for(int j=0;j<=k;j++)
42       d[i][j]=inf;
43     memset(v,0,sizeof(v));
44     d[1][0]=0;q.push(make_pair(0,1));
45     while(!q.empty())
46     {
47         int x=q.top().second;q.pop();
48         int z=x/(n+1);x=x%(n+1);
49         if(v[x][z])continue;v[x][z]=1;
50         for(int i=head[x],y;i;i=e[i].next)
51           {
52             if(d[x][z]+e[i].w<d[y=e[i].go][z])
53             {
54                 d[y][z]=d[x][z]+e[i].w;
55                 q.push(make_pair(d[y][z],z*(n+1)+y));
56             }
57             if(z==k)continue;
58             if(d[x][z]<d[y][z+1])
59             {
60                 d[y][z+1]=d[x][z];
61                 q.push(make_pair(d[y][z+1],(z+1)*(n+1)+y));
62             }
63           } 
64     }
65 }
66 int main()
67 {
68     freopen("input.txt","r",stdin);
69     freopen("output.txt","w",stdout);
70     n=read();m=read();k=read();
71     for(int i=1;i<=m;i++)
72     {
73         int x=read(),y=read(),z=read();
74         insert(x,y,z);
75     }
76     dijkstra();
77     printf("%d
",d[n][k]);
78     return 0;
79 }
View Code