http://acm.hdu.edu.cn/showproblem.php?pid=4396
题意:
求 从 一点s 到 e 至少经过k 条边求 最短的值是?(可以走重边);
题接:
二维最短路 ,d[i][k] 表示 从 s 到 i 点经过 k 条边 的最小值 ,(对于 大于k边数的值 ,并入到 dp[i][k]里 面 )
1 #include<cstdio>
2 #include<cstring>
3 #include<cmath>
4 #include<iostream>
5 #include<algorithm>
6 #include<set>
7 #include<map>
8 #include<queue>
9 #include<vector>
10 #include<string>
11 #define Min(a,b) a<b?a:b
12 #define Max(a,b) a>b?a:b
13 #define CL(a,num) memset(a,num,sizeof(a));
14 #define maxn 5010
15 #define eps 1e-6
16 #define inf 9999999
17 #define mx 1<<60
18 using namespace std;
19 struct node
20 {
21 int v;
22 int len;
23 int next ;
24 }p[maxn*50];
25 int num;
26 int next[maxn] ,head[maxn];
27 void add(int u,int v,int len)
28 {
29 p[num].v = v;
30 p[num].len = len;
31 p[num].next = next[u];
32 next[u] = num ++;
33 }
34 void init()
35 {
36 num = 0;
37 CL(next,-1);
38 }
39 struct qnode
40 {
41 int u;
42 int time;
43 int eg;
44 qnode(int x,int y,int z):u(x),time(y),eg(z){}
45 };
46 int dp[maxn][60] ,vis[maxn][60],n,m;
47 void SPFA(int s,int e,int k)
48 {
49 int i,j;
50 queue<qnode>que;
51 CL(vis,0);
52 for( i = 0;i<=n; i++)
53 {
54 for(j = 0; j < 60;j++)
55 dp[i][j] = inf;
56 }
57
58 que.push(qnode(s,0,0));
59 vis[s][0] = 1;
60
61 dp[s][0] = 0 ;
62
63 while(!que.empty())
64 {
65 qnode a = que.front();que.pop();
66
67 int u = a.u;
68 int time = a.time ;
69 int eg = a.eg;
70 vis[u][eg] = 0;
71 for(i = next[u];i!= -1;i = p[i].next)
72 {
73 int v = p[i].v;
74 int len = p[i].len;
75 int cost = dp[u][eg] + len ;
76 int en;
77 if(eg +1 >k) en = k;
78 else en = eg+1;
79
80 if(dp[v][en] > cost)
81 {
82
83 dp[v][en] = cost ;
84
85 if(!vis[v][en])
86 {
87 vis[v][en] = 1;
88 que.push(qnode(v,dp[v][en],en)) ;
89 }
90 }
91 }
92 }
93
94 }
95 int main()
96 {
97 int x,y,len,s,e,k,i;
98 //freopen("data.in","r",stdin);
99 while(scanf("%d%d",&n,&m)!=EOF)
100 {
101 init() ;
102 for(i = 0;i< m;i++)
103 {
104 scanf("%d%d%d",&x,&y,&len);
105 add(x,y,len);
106 add(y,x,len);
107 }
108 scanf("%d%d%d",&s,&e,&k);
109
110 k = (k + 9) /10 ;
111 SPFA( s, e, k);
112
113 if(dp[e][k] != inf)printf("%d\n",dp[e][k]);
114 else printf("-1\n");
115 }
116 return 0;
117
118 }
2 #include<cstring>
3 #include<cmath>
4 #include<iostream>
5 #include<algorithm>
6 #include<set>
7 #include<map>
8 #include<queue>
9 #include<vector>
10 #include<string>
11 #define Min(a,b) a<b?a:b
12 #define Max(a,b) a>b?a:b
13 #define CL(a,num) memset(a,num,sizeof(a));
14 #define maxn 5010
15 #define eps 1e-6
16 #define inf 9999999
17 #define mx 1<<60
18 using namespace std;
19 struct node
20 {
21 int v;
22 int len;
23 int next ;
24 }p[maxn*50];
25 int num;
26 int next[maxn] ,head[maxn];
27 void add(int u,int v,int len)
28 {
29 p[num].v = v;
30 p[num].len = len;
31 p[num].next = next[u];
32 next[u] = num ++;
33 }
34 void init()
35 {
36 num = 0;
37 CL(next,-1);
38 }
39 struct qnode
40 {
41 int u;
42 int time;
43 int eg;
44 qnode(int x,int y,int z):u(x),time(y),eg(z){}
45 };
46 int dp[maxn][60] ,vis[maxn][60],n,m;
47 void SPFA(int s,int e,int k)
48 {
49 int i,j;
50 queue<qnode>que;
51 CL(vis,0);
52 for( i = 0;i<=n; i++)
53 {
54 for(j = 0; j < 60;j++)
55 dp[i][j] = inf;
56 }
57
58 que.push(qnode(s,0,0));
59 vis[s][0] = 1;
60
61 dp[s][0] = 0 ;
62
63 while(!que.empty())
64 {
65 qnode a = que.front();que.pop();
66
67 int u = a.u;
68 int time = a.time ;
69 int eg = a.eg;
70 vis[u][eg] = 0;
71 for(i = next[u];i!= -1;i = p[i].next)
72 {
73 int v = p[i].v;
74 int len = p[i].len;
75 int cost = dp[u][eg] + len ;
76 int en;
77 if(eg +1 >k) en = k;
78 else en = eg+1;
79
80 if(dp[v][en] > cost)
81 {
82
83 dp[v][en] = cost ;
84
85 if(!vis[v][en])
86 {
87 vis[v][en] = 1;
88 que.push(qnode(v,dp[v][en],en)) ;
89 }
90 }
91 }
92 }
93
94 }
95 int main()
96 {
97 int x,y,len,s,e,k,i;
98 //freopen("data.in","r",stdin);
99 while(scanf("%d%d",&n,&m)!=EOF)
100 {
101 init() ;
102 for(i = 0;i< m;i++)
103 {
104 scanf("%d%d%d",&x,&y,&len);
105 add(x,y,len);
106 add(y,x,len);
107 }
108 scanf("%d%d%d",&s,&e,&k);
109
110 k = (k + 9) /10 ;
111 SPFA( s, e, k);
112
113 if(dp[e][k] != inf)printf("%d\n",dp[e][k]);
114 else printf("-1\n");
115 }
116 return 0;
117
118 }