最短路练习

这几天做了一个最短路的复习,都是最简单的最短路dijkstra,floyd,A_Star,SPFA

地址http://vjudge.net/contest/view.action?cid=50053#problem/G

A:HDU 2544

最基本,贴代码

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define mem0(a) memset(a, 0, sizeof(a))
 5 using namespace std;
 6 int MIN(int a, int b)
 7 {
 8     return a < b ? a : b;
 9 }
10 
11 const int INF = 0x3f3f3f3f;
12 const int MAXN = 110;
13 
14 int N, M;
15 int edge[MAXN][MAXN];
16 int d[MAXN], vis[MAXN];
17 
18 int dijkstra(int s)
19 {
20     mem0(vis);
21     for(int i=0;i<=N;i++) d[i] = INF;
22     d[s] = 0;
23     for(int i=1;i<=N;i++)
24     {
25         int m = INF;
26         for(int j=1;j<=N;j++)if(!vis[j] && d[j] < m) m = d[s = j];
27         vis[s] = 1;
28         for(int j=1;j<=N;j++)if(d[j]>d[s]+edge[s][j])
29         {
30             d[j] = d[s] + edge[s][j];
31         }
32     }
33     return d[N];
34 }
35 
36 int main()
37 {
38     while(~scanf("%d %d", &N, &M) && (N || M))
39     {
40         for(int i=0;i<=N;i++)for(int j=0;j<=N;j++)
41             edge[i][j] = INF;
42         int a,b,c;
43         for(int i=0;i<M;i++)
44         {
45             scanf("%d %d %d", &a, &b, &c);
46             edge[a][b] = edge[b][a] = MIN(edge[a][b], c);
47         }
48         printf("%d\n", dijkstra(1));
49     }
50     return 0;
51 }
View Code

 

B:HDU 2122

同上

 1 #include <map>
 2 #include <set>
 3 #include <stack>
 4 #include <queue>
 5 #include <cmath>
 6 #include <ctime>
 7 #include <vector>
 8 #include <cstdio>
 9 #include <cctype>
10 #include <cstring>
11 #include <cstdlib>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 #define INF 1e10
16 #define inf (-((LL)1<<40))
17 #define lson k<<1, L, mid
18 #define rson k<<1|1, mid+1, R
19 #define mem0(a) memset(a,0,sizeof(a))
20 #define mem1(a) memset(a,-1,sizeof(a))
21 #define mem(a, b) memset(a, b, sizeof(a))
22 #define FOPENIN(IN) freopen(IN, "r", stdin)
23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout)
24 template<class T> T CMP_MIN(T a, T b) { return a < b; }
25 template<class T> T CMP_MAX(T a, T b) { return a > b; }
26 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
27 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
28 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
29 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
30 
31 //typedef __int64 LL;
32 //typedef long long LL;
33 const int MAXN = 200005;
34 const int MAXM = 2000005;
35 const double eps = 1e-12;
36 
37 int p[MAXN];
38 int N, M;
39 struct NODE
40 {
41     int x, y;
42     int c;
43     NODE(){}
44     NODE(int _x, int _y, int _c)
45     {
46         x = _x; y = _y; c = _c;
47     }
48 }edge[MAXM];
49 
50 int cmp(NODE A, NODE B)
51 {
52     return A.c < B.c;
53 }
54 
55 int findP(int x)
56 {
57     return x == p[x] ? x : p[x] = findP(p[x]);
58 }
59 
60 
61 int main()
62 {
63     while(~scanf("%d %d", &N, &M))
64     {
65         int a, b, c;
66         for(int i=0;i<=N;i++)p[i] = i;
67         for(int i=0;i<M;i++)
68         {
69             scanf("%d %d %d", &a, &b, &c);
70             edge[i] = NODE(a, b, c);
71         }
72         sort(edge, edge + M, cmp);
73         int cnt = N, ans = 0;
74         for(int i=0;i<M;i++)
75         {
76             int x = findP(edge[i].x);
77             int y = findP(edge[i].y);
78             if(x != y)
79             {
80                 cnt --;
81                 p[x] = y;
82                 ans += edge[i].c;
83             }
84         }
85         if(cnt > 1) printf("impossible\n\n");
86         else printf("%d\n\n", ans);
87     }
88     return 0;
89 }
View Code

 

C:HDU 1874

做了N遍了

 1 #include <map>
 2 #include <set>
 3 #include <stack>
 4 #include <queue>
 5 #include <cmath>
 6 #include <ctime>
 7 #include <vector>
 8 #include <cstdio>
 9 #include <cctype>
10 #include <cstring>
11 #include <cstdlib>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 #define INF 1e9
16 #define inf (-((LL)1<<40))
17 #define lson k<<1, L, mid
18 #define rson k<<1|1, mid+1, R
19 #define mem0(a) memset(a,0,sizeof(a))
20 #define mem1(a) memset(a,-1,sizeof(a))
21 #define mem(a, b) memset(a, b, sizeof(a))
22 #define FOPENIN(IN) freopen(IN, "r", stdin)
23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout)
24 template<class T> T CMP_MIN(T a, T b) { return a < b; }
25 template<class T> T CMP_MAX(T a, T b) { return a > b; }
26 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
27 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
28 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
29 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
30 
31 //typedef __int64 LL;
32 //typedef long long LL;
33 const int MAXN = 205;
34 const int MAXM = 1100;
35 const double eps = 1e-12;
36 
37 int N, M, S, T;
38 int edge[MAXN][MAXN];
39 int d[MAXN], vis[MAXN];
40 
41 int dijkstra(int s)
42 {
43     for(int i=0;i<=N;i++) d[i] = INF;
44     d[s] = 0;mem0(vis);
45     for(int i=0;i<N;i++)
46     {
47         int m = INF;
48         for(int j=0;j<N;j++) if(!vis[j] && m > d[j]) m = d[s = j];
49         vis[s] = 1;
50         for(int j=0;j<N;j++) if(d[j] > d[s] + edge[s][j])
51             d[j] = d[s] + edge[s][j];
52     }
53     if(d[T] == INF) return -1;
54     return d[T];
55 }
56 
57 int main()
58 {
59     while(~scanf("%d %d", &N, &M))
60     {
61         int a, b, c;
62         for(int i=0;i<N;i++)for(int j=0;j<N;j++) edge[i][j] = INF;
63         for(int i=0;i<M;i++)
64         {
65             scanf("%d %d %d", &a, &b, &c);
66             edge[a][b] = edge[b][a] = MIN(edge[a][b], c);
67         }
68         scanf("%d %d", &S, &T);
69         printf("%d\n", dijkstra(S));
70     }
71     return 0;
72 }
View Code

 

D:HDU 1142

同样是最短路,只是最初理解为求最短路的条数,实际上是说如果存在两个点A,B,满足A到终点的最短距离小于等于B到终点的最短距离,那就可以从A走到B,现在问从起点到终点有多少种走法。

解决办法依然是最短路,从终点出发求一遍最短路,得出终点到其他任意定点的最短路D[i], 那么再从起点DFS一遍,统计遍历时满足D[u] > D[v](u是v的前驱结点)的条数,递归求解即可。

 1 #include <map>
 2 #include <set>
 3 #include <stack>
 4 #include <queue>
 5 #include <cmath>
 6 #include <ctime>
 7 #include <vector>
 8 #include <cstdio>
 9 #include <cctype>
10 #include <cstring>
11 #include <cstdlib>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 #define INF 1e9
16 #define inf (-((LL)1<<40))
17 #define lson k<<1, L, mid
18 #define rson k<<1|1, mid+1, R
19 #define mem0(a) memset(a,0,sizeof(a))
20 #define mem1(a) memset(a,-1,sizeof(a))
21 #define mem(a, b) memset(a, b, sizeof(a))
22 #define FOPENIN(IN) freopen(IN, "r", stdin)
23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout)
24 template<class T> T CMP_MIN(T a, T b) { return a < b; }
25 template<class T> T CMP_MAX(T a, T b) { return a > b; }
26 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
27 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
28 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
29 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
30 
31 //typedef __int64 LL;
32 //typedef long long LL;
33 const int MAXN = 2005;
34 const int MAXM = 110000;
35 const double eps = 1e-12;
36 
37 int N, M;
38 int edge[MAXN][MAXN];
39 int d[MAXN], vis[MAXN], cnt[MAXN];
40 
41 void dijkstra(int s)
42 {
43     mem0(vis);
44     for(int i=0;i<=N;i++) d[i] = INF;
45     d[s] = 0;
46     for(int i=1;i<N;i++)
47     {
48         int m = INF;
49         for(int j=1;j<=N;j++) if(!vis[j] && m > d[j]) m = d[s = j];
50         vis[s] = 1;
51         for(int j=1;j<=N;j++) if(d[j] > d[s] + edge[s][j])
52         {
53             d[j] = d[s] + edge[s][j];
54         }
55     }
56 }
57 
58 void DFS(int cur)
59 {
60     if(cur == 2)
61     {
62         cnt[cur] = 1;
63         return ;
64     }
65     for(int i=1;i<=N;i++) if(edge[cur][i] != INF && d[cur] > d[i])
66     {
67         if(!cnt[i]) DFS(i);
68         cnt[cur] += cnt[i];
69     }
70 }
71 
72 int main()
73 {
74     while(~scanf("%d", &N) && N)
75     {
76         scanf("%d", &M);
77         int a, b, c;
78         for(int i=0;i<=N;i++)for(int j=0;j<=N;j++) edge[i][j] = INF;
79         for(int i=0;i<M;i++)
80         {
81             scanf("%d %d %d", &a, &b, &c);
82             edge[a][b] = edge[b][a] = MIN(edge[a][b], c);
83         }
84         dijkstra(2);
85         mem0(cnt);
86         DFS(1);
87         printf("%d\n", cnt[1]);
88     }
89     return 0;
90 }
View Code

 

E:HDU 1385

可以有多种方法,数据很水,即使是没输入一次就进行一次最短路的记录都不会超时

这里学了一个floyd的路径记录,即记录从任意定点u到任意顶点v的最短路径:

给出的方法是定义一个next[i][j],用来表示从i到j的最短路径上的下一个节点,就是说当前在点i要到达j且路径最短,那么下一个点应该走next[i][j]

 1 void floyd()
 2 {
 3     for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) next[i][j] = j;
 4 
 5     for(int k=1;k<=N;k++) for(int i=1;i<=N;i++)
 6     for(int j=1;j<=N;j++)
 7     {
 8         int sum = a[i][k] + a[k][j] + b[k];
 9         if(a[i][j] > sum)
10         {
11             a[i][j] = sum;
12             next[i][j] = next[i][k];
13         }
14         else if(a[i][j] == sum)
15         {
16             next[i][j] = MIN(next[i][j], next[i][k]);
17         }
18     }
19 }
 1 #include <map>
 2 #include <set>
 3 #include <stack>
 4 #include <queue>
 5 #include <cmath>
 6 #include <ctime>
 7 #include <vector>
 8 #include <cstdio>
 9 #include <cctype>
10 #include <cstring>
11 #include <cstdlib>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 #define INF 1e9
16 #define inf (-((LL)1<<40))
17 #define lson k<<1, L, mid
18 #define rson k<<1|1, mid+1, R
19 #define mem0(a) memset(a,0,sizeof(a))
20 #define mem1(a) memset(a,-1,sizeof(a))
21 #define mem(a, b) memset(a, b, sizeof(a))
22 #define FOPENIN(IN) freopen(IN, "r", stdin)
23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout)
24 template<class T> T CMP_MIN(T a, T b) { return a < b; }
25 template<class T> T CMP_MAX(T a, T b) { return a > b; }
26 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
27 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
28 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
29 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
30 
31 //typedef __int64 LL;
32 //typedef long long LL;
33 const int MAXN = 205;
34 const int MAXM = 110000;
35 const double eps = 1e-12;
36 
37 int N, S, T;
38 int a[MAXN][MAXN], b[MAXN], next[MAXN][MAXN];
39 
40 void floyd()
41 {
42     for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) next[i][j] = j;
43 
44     for(int k=1;k<=N;k++) for(int i=1;i<=N;i++)
45     for(int j=1;j<=N;j++)
46     {
47         int sum = a[i][k] + a[k][j] + b[k];
48         if(a[i][j] > sum)
49         {
50             a[i][j] = sum;
51             next[i][j] = next[i][k];
52         }
53         else if(a[i][j] == sum)
54         {
55             next[i][j] = MIN(next[i][j], next[i][k]);
56         }
57     }
58 }
59 
60 int main()
61 {
62     while(~scanf("%d", &N) && N)
63     {
64         for(int i=1;i<=N;i++)
65         for(int j=1;j<=N;j++)
66         {
67             scanf("%d", &a[i][j]);
68             if(a[i][j] == -1) a[j][i] = INF;
69         }
70         for(int i=1;i<=N;i++)
71         {
72             scanf("%d", &b[i]);
73         }
74 
75         floyd();
76 
77         while(scanf("%d %d", &S, &T) && S != -1)
78         {
79             printf("From %d to %d :\nPath: ", S, T);
80 
81             int  s = S;
82             while(s != T)
83             {
84                 printf("%d-->", s);
85                 s = next[s][T];
86             }
87             printf("%d\n", T);
88 
89             printf("Total cost : %d\n\n", a[S][T]);
90         }
91     }
92     return 0;
93 }
View Code

 

F:HDU 1548

宽搜一下

 1 #include <map>
 2 #include <set>
 3 #include <stack>
 4 #include <queue>
 5 #include <cmath>
 6 #include <ctime>
 7 #include <vector>
 8 #include <cstdio>
 9 #include <cctype>
10 #include <cstring>
11 #include <cstdlib>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 #define INF 1e9
16 #define inf (-((LL)1<<40))
17 #define lson k<<1, L, mid
18 #define rson k<<1|1, mid+1, R
19 #define mem0(a) memset(a,0,sizeof(a))
20 #define mem1(a) memset(a,-1,sizeof(a))
21 #define mem(a, b) memset(a, b, sizeof(a))
22 #define FOPENIN(IN) freopen(IN, "r", stdin)
23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout)
24 template<class T> T CMP_MIN(T a, T b) { return a < b; }
25 template<class T> T CMP_MAX(T a, T b) { return a > b; }
26 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
27 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
28 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
29 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
30 
31 //typedef __int64 LL;
32 //typedef long long LL;
33 const int MAXN = 205;
34 const int MAXM = 110000;
35 const double eps = 1e-12;
36 
37 int N, A, B;
38 int fl[MAXN], step[MAXN];
39 bool vis[MAXN];
40 
41 int BFS()
42 {
43     mem0(vis);
44     queue<int>q;
45     q.push(A);
46     vis[A] = 1;
47     step[A] = 0;
48     while(!q.empty())
49     {
50         int f = q.front(); q.pop();
51         if(f == B) return step[B];
52         int up = f + fl[f];
53         if(up <= N && !vis[up])
54         {
55             step[up] = step[f] + 1;
56             vis[up] = 1;
57             q.push(up);
58         }
59         int down = f - fl[f];
60         if(down > 0 && !vis[down])
61         {
62             step[down] = step[f] + 1;
63             vis[down] = 1;
64             q.push(down);
65         }
66     }
67     return -1;
68 }
69 
70 int main()
71 {
72     while(~scanf("%d", &N) && N)
73     {
74         scanf("%d %d", &A, &B);
75         for(int i=1;i<=N;i++) scanf("%d", &fl[i]);
76         printf("%d\n", BFS());
77     }
78     return 0;
79 }
View Code

 

G:HDU 2923

水题,floyd

 1 #include <map>
 2 #include <set>
 3 #include <stack>
 4 #include <queue>
 5 #include <cmath>
 6 #include <ctime>
 7 #include <vector>
 8 #include <cstdio>
 9 #include <cctype>
10 #include <cstring>
11 #include <cstdlib>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 #define INF 1e9
16 #define inf (-((LL)1<<40))
17 #define lson k<<1, L, mid
18 #define rson k<<1|1, mid+1, R
19 #define mem0(a) memset(a,0,sizeof(a))
20 #define mem1(a) memset(a,-1,sizeof(a))
21 #define mem(a, b) memset(a, b, sizeof(a))
22 #define FOPENIN(IN) freopen(IN, "r", stdin)
23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout)
24 template<class T> T CMP_MIN(T a, T b) { return a < b; }
25 template<class T> T CMP_MAX(T a, T b) { return a > b; }
26 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
27 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
28 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
29 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
30 
31 //typedef __int64 LL;
32 //typedef long long LL;
33 const int MAXN = 205;
34 const int MAXM = 110000;
35 const double eps = 1e-12;
36 
37 int N, C, R;
38 map<string, int>myMap;
39 int D[110][110];
40 char location[1100][500];
41 
42 void floyd()
43 {
44     for(int k=1;k<=N;k++) for(int i=1;i<=N;i++)
45     for(int j=1;j<=N;j++)
46     {
47         D[i][j] = MIN(D[i][j], D[i][k] + D[k][j]);
48     }
49 }
50 
51 int main()
52 {
53     int test = 0;
54     while(~scanf("%d %d %d%*c", &N, &C, &R) && (N||C||R))
55     {
56         myMap.clear();
57         for(int i=0;i<=N;i++)
58             for(int j=0;j<=N;j++)
59                 D[i][j] = INF;
60 
61         for(int i=0;i<=C;i++) scanf("%s", location[i]);
62 
63         int cnt = 1, dis;
64         char from[50], left, right, to[50];
65         for(int i=0;i<R;i++)
66         {
67             scanf("%s %c-%d-%c %s", from, &left, &dis, &right, to);
68             if(!myMap[from])
69                 myMap[from] = cnt ++;
70             if(!myMap[to])
71                 myMap[to] = cnt ++;
72             int x = myMap[from], y = myMap[to];
73             if(left=='<' && dis < D[y][x])
74                 D[y][x] = dis;
75             if(right=='>' && dis < D[x][y])
76                 D[x][y] = dis;
77         }
78 
79         floyd();
80 
81         int start = myMap[location[0]], sum = 0;
82         for(int i=1;i<=C;i++)
83         {
84             int des = myMap[location[i]];
85             sum += D[start][des] + D[des][start];
86         }
87 
88         printf("%d. %d\n", ++test, sum);
89     }
90     return 0;
91 }
View Code

 

H:HDU 2962

求这样一个H值,使得H值小于经过的没一条边的H值,首先使得满足条件时H值最大,如果从起点到终点有多条H值得路径,要使得路径最短

我是这样做的,首先乱搞一下,求出起点到终点路径上的最大的H值,这里的过程其实就是模仿最短路,设H[u]代表从起点到达u时使得沿途的最小H值尽量大的H值,每次选择当前H值最大的顶点u,从它开始扩展,那么从起点到达u的下一个顶点v, H[v] = MAX(  H[v],  MIN(H[u],  limit[u][v])  );H初始化为0

得到最大可以达到的H后直接求最短路就可以了(加一个限制就是走的边的H值小于MaxH)

  1 #include <map>
  2 #include <set>
  3 #include <stack>
  4 #include <queue>
  5 #include <cmath>
  6 #include <ctime>
  7 #include <vector>
  8 #include <cstdio>
  9 #include <cctype>
 10 #include <cstring>
 11 #include <cstdlib>
 12 #include <iostream>
 13 #include <algorithm>
 14 using namespace std;
 15 #define INF 1e9
 16 #define inf (-((LL)1<<40))
 17 #define lson k<<1, L, mid
 18 #define rson k<<1|1, mid+1, R
 19 #define mem0(a) memset(a,0,sizeof(a))
 20 #define mem1(a) memset(a,-1,sizeof(a))
 21 #define mem(a, b) memset(a, b, sizeof(a))
 22 #define FOPENIN(IN) freopen(IN, "r", stdin)
 23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout)
 24 template<class T> T CMP_MIN(T a, T b) { return a < b; }
 25 template<class T> T CMP_MAX(T a, T b) { return a > b; }
 26 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
 27 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
 28 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
 29 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
 30 
 31 //typedef __int64 LL;
 32 //typedef long long LL;
 33 const int MAXN = 205;
 34 const int MAXM = 110000;
 35 const double eps = 1e-12;
 36 
 37 int C, R, S, T, MH;
 38 int H[1100][1100], W[1100][1100];
 39 int D[1100], vis[1100];
 40 
 41 int LG()
 42 {
 43     mem0(vis); mem0(D);
 44     D[T] = INF;
 45     for(int i=1;i<C;i++)
 46     {
 47         int m = 0, s;
 48         for(int j=1;j<=C;j++) if(!vis[j] && m < D[j]) m = D[s = j];
 49         vis[s] = 1;
 50         for(int j=1;j<=C;j++) if(H[s][j])
 51         {
 52             D[j] = MAX(D[j], MIN(D[s], H[s][j]));
 53         }
 54     }
 55     return D[S];
 56 }
 57 
 58 int dijkstra()
 59 {
 60     for(int i=0;i<=C;i++)
 61     {
 62         vis[i] = 0;
 63         D[i] = INF;
 64     }
 65     D[S] = 0;
 66     for(int i=1;i<C;i++)
 67     {
 68         int m = INF, s;
 69         for(int j=1;j<=C;j++) if(!vis[j] && m > D[j]) m = D[s = j];
 70         vis[s] = 1;
 71         for(int j=1;j<=C;j++) if(H[s][j] >= MH && D[j] > D[s] + W[s][j])
 72         {
 73             D[j] = D[s] + W[s][j];
 74         }
 75     }
 76     return D[T];
 77 }
 78 
 79 int main()
 80 {
 81     int cas = 1;
 82     while(~scanf("%d %d", &C, &R) && (C || R))
 83     {
 84         if(cas > 1) printf("\n");
 85         mem0(H); mem0(W);
 86         int a, b, h, l;
 87         for(int i=0;i<R;i++)
 88         {
 89             scanf("%d %d %d %d", &a, &b, &h, &l);
 90             if(h == -1) h = INF;
 91             H[a][b] = H[b][a] = h;
 92             W[a][b] = W[b][a] = l;
 93         }
 94         scanf("%d %d %d", &S, &T, &MH);
 95         MH = MIN(MH, LG());
 96         printf("Case %d:\n", cas ++);
 97         if(MH == 0) printf("cannot reach destination\n");
 98         else printf("maximum height = %d\nlength of shortest route = %d\n", MH, dijkstra());
 99     }
100     return 0;
101 }
View Code

 

I:HDU 2722

水题

  1 #include <map>
  2 #include <set>
  3 #include <stack>
  4 #include <queue>
  5 #include <cmath>
  6 #include <ctime>
  7 #include <vector>
  8 #include <cstdio>
  9 #include <cctype>
 10 #include <cstring>
 11 #include <cstdlib>
 12 #include <iostream>
 13 #include <algorithm>
 14 using namespace std;
 15 #define INF 1e9
 16 #define inf (-((LL)1<<40))
 17 #define lson k<<1, L, mid
 18 #define rson k<<1|1, mid+1, R
 19 #define mem0(a) memset(a,0,sizeof(a))
 20 #define mem1(a) memset(a,-1,sizeof(a))
 21 #define mem(a, b) memset(a, b, sizeof(a))
 22 #define FOPENIN(IN) freopen(IN, "r", stdin)
 23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout)
 24 template<class T> T CMP_MIN(T a, T b) { return a < b; }
 25 template<class T> T CMP_MAX(T a, T b) { return a > b; }
 26 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
 27 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
 28 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
 29 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
 30 
 31 typedef __int64 LL;
 32 //typedef long long LL;
 33 const int MAXN = 1100;
 34 const int MAXM = 11000;
 35 const double eps = 1e-10;
 36 const LL MOD = 1000000007;
 37 
 38 int N, M, tot;
 39 int Map[MAXN][MAXN];
 40 
 41 void addEdge(int x, int y,  int val, char dir)
 42 {
 43     if(val == 0) return ;
 44     int time  = 2520 / val;
 45     if(dir == '*') Map[x][y] = Map[y][x] = time;
 46     else if(dir == '<') { Map[y][x] = time; Map[x][y] = INF; }
 47     else if(dir == '>') { Map[y][x] = INF; Map[x][y] = time; }
 48     else if(dir == '^') { Map[y][x] = time; Map[x][y] = INF; }
 49     else if(dir == 'v') { Map[y][x] = INF; Map[x][y] = time; }
 50 }
 51 
 52 int vis[MAXN], d[MAXN];
 53 int dijkstra(int s)
 54 {
 55     mem0(vis);
 56     for(int i=0;i<=tot;i++) d[i] = INF;
 57     d[s] = 0;
 58     for(int i=1;i<=tot;i++)
 59     {
 60         int m = INF;
 61         for(int j=0;j<tot;j++)if(!vis[j] && d[j] < m) m = d[s = j];
 62         vis[s] = 1;
 63         for(int j=0;j<tot;j++)if(d[j]>d[s]+Map[s][j])
 64         {
 65             d[j] = d[s] + Map[s][j];
 66         }
 67     }
 68     return d[tot-1];
 69 }
 70 
 71 int main()
 72 {
 73     while(~scanf("%d %d", &N, &M) && (N||M))
 74     {
 75         int val;
 76         char dir;
 77         for(int i=0;i<(N+1)*(M+1);i++)
 78             for(int j=0;j<(N+1)*(M+1);j++)
 79                 Map[i][j] = INF;
 80         for(int i=0;i<=N;i++)
 81         {
 82             for(int j=0;j<M;j++)
 83             {
 84                 int x = i * (M+1) + j, y = x + 1;
 85                 scanf("%d %c", &val, &dir);
 86                 addEdge(x, y, val, dir);
 87             }
 88             if(i<N) for(int j=0;j<=M;j++)
 89             {
 90                 int x = i * (M+1) + j, y = x + M + 1;
 91                 scanf("%d %c", &val, &dir);
 92                 addEdge(x, y, val, dir);
 93             }
 94         }
 95         tot = (N+1) * (M+1) ;
 96         int ans = dijkstra(0);
 97         if(ans == INF) printf("Holiday\n");
 98         else printf("%d blips\n", ans);
 99     }
100     return 0;
101 }
View Code

 

J:HDU 1690

水题

 1 #include <map>
 2 #include <set>
 3 #include <stack>
 4 #include <queue>
 5 #include <cmath>
 6 #include <ctime>
 7 #include <vector>
 8 #include <cstdio>
 9 #include <cctype>
10 #include <cstring>
11 #include <cstdlib>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 #define INF 1e12
16 #define inf (-((LL)1<<40))
17 #define lson k<<1, L, mid
18 #define rson k<<1|1, mid+1, R
19 #define mem0(a) memset(a,0,sizeof(a))
20 #define mem1(a) memset(a,-1,sizeof(a))
21 #define mem(a, b) memset(a, b, sizeof(a))
22 #define FOPENIN(IN) freopen(IN, "r", stdin)
23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout)
24 template<class T> T CMP_MIN(T a, T b) { return a < b; }
25 template<class T> T CMP_MAX(T a, T b) { return a > b; }
26 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
27 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
28 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
29 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
30 
31 typedef __int64 LL;
32 //typedef long long LL;
33 const int MAXN = 1100;
34 const int MAXM = 11000;
35 const double eps = 1e-10;
36 const LL MOD = 1000000007;
37 
38 LL L1, L2, L3, L4, C1, C2, C3, C4;
39 int N, M;
40 LL Map[MAXN][MAXN], pos[MAXN];
41 
42 LL getCost(LL dist)
43 {
44     if(dist == 0) return 0;
45     if(dist > 0 && dist <= L1) return C1;
46     if(dist > L1 && dist <= L2) return C2;
47     if(dist > L2 && dist <= L3) return C3;
48     if(dist > L3 && dist <= L4) return C4;
49     return INF;
50 }
51 
52 void floyd()
53 {
54     for(int k=1;k<=N;k++)
55         for(int i=1;i<=N;i++)
56             for(int j=1;j<=N;j++)
57                 Map[i][j] = MIN(Map[i][j], Map[i][k] + Map[k][j]);
58 }
59 
60 int main()
61 {
62     int T, t = 0;
63     while(~scanf("%d", &T)) while(T--)
64     {
65         scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d", &L1, &L2, &L3, &L4, &C1, &C2, &C3, &C4);
66         scanf("%d %d", &N, &M);
67         for(int i=1;i<=N;i++) scanf("%I64d", &pos[i]);
68         for(int i=1;i<=N;i++)
69             for(int j=1;j<=N;j++)
70             {
71                 LL tt = pos[i]-pos[j];
72                 if(tt < 0) tt = -tt;
73                 Map[i][j] = getCost(tt);
74             }
75         floyd();
76         int a , b;
77         printf("Case %d:\n", ++t);
78         while(M--)
79         {
80             scanf("%d %d", &a, &b);
81             if(Map[a][b] == INF) printf("Station %d and station %d are not attainable.\n", a, b);
82             else printf("The minimum cost between station %d and station %d is %I64d.\n", a, b, Map[a][b]);
83         }
84     }
85     return 0;
86 }
View Code

 

L:HDU 1596

水题

 1 #include <map>
 2 #include <set>
 3 #include <stack>
 4 #include <queue>
 5 #include <cmath>
 6 #include <ctime>
 7 #include <vector>
 8 #include <cstdio>
 9 #include <cctype>
10 #include <cstring>
11 #include <cstdlib>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 #define INF 1e9
16 #define inf (-((LL)1<<40))
17 #define lson k<<1, L, mid
18 #define rson k<<1|1, mid+1, R
19 #define mem0(a) memset(a,0,sizeof(a))
20 #define mem1(a) memset(a,-1,sizeof(a))
21 #define mem(a, b) memset(a, b, sizeof(a))
22 #define FOPENIN(IN) freopen(IN, "r", stdin)
23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout)
24 template<class T> T CMP_MIN(T a, T b) { return a < b; }
25 template<class T> T CMP_MAX(T a, T b) { return a > b; }
26 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
27 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
28 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
29 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
30 
31 typedef __int64 LL;
32 //typedef long long LL;
33 const int MAXN = 1100;
34 const int MAXM = 11000;
35 const double eps = 1e-10;
36 
37 int N, M;
38 double D[MAXN], a[MAXN][MAXN];
39 int vis[MAXN];
40 
41 double dijkstra(int s, int t)
42 {
43     for(int i=0;i<=N;i++)
44     {
45         D[i] = a[s][i];
46         vis[i] = 0;
47     }
48     vis[s] = 1;
49     for(int i=1;i<=N;i++)
50     {
51         double m = 0;
52         for(int j=1;j<=N;j++) if(!vis[j] && D[j]>m ) m = D[s = j];
53         if(m == 0) return D[t];
54         vis[s] = 1;
55         for(int j=1;j<=N;j++)if(!vis[j] && D[s]*a[s][j] > D[j])
56             D[j] = D[s]*a[s][j];
57     }
58     return D[t];
59 }
60 
61 int main()
62 {
63     while(~scanf("%d", &N))
64     {
65         for(int i=1;i<=N;i++)
66         {
67             for(int j=1;j<=N;j++)
68             {
69                 scanf("%lf", &a[i][j]);
70             }
71         }
72         scanf("%d", &M);
73         while(M--)
74         {
75             int s, t;
76             scanf("%d %d", &s, &t);
77             double ans = dijkstra(s, t);
78             if(ans == 0.0) printf("What a pity!\n");
79             else printf("%.3lf\n", ans);
80         }
81     }
82     return 0;
83 }
View Code

 

M:HDU 1598

枚举+并查集

 1 #include <map>
 2 #include <set>
 3 #include <stack>
 4 #include <queue>
 5 #include <cmath>
 6 #include <ctime>
 7 #include <vector>
 8 #include <cstdio>
 9 #include <cctype>
10 #include <cstring>
11 #include <cstdlib>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 #define INF 1e9
16 #define inf (-((LL)1<<40))
17 #define lson k<<1, L, mid
18 #define rson k<<1|1, mid+1, R
19 #define mem0(a) memset(a,0,sizeof(a))
20 #define mem1(a) memset(a,-1,sizeof(a))
21 #define mem(a, b) memset(a, b, sizeof(a))
22 #define FOPENIN(IN) freopen(IN, "r", stdin)
23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout)
24 template<class T> T CMP_MIN(T a, T b) { return a < b; }
25 template<class T> T CMP_MAX(T a, T b) { return a > b; }
26 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
27 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
28 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
29 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
30 
31 typedef __int64 LL;
32 //typedef long long LL;
33 const int MAXN = 2005;
34 const int MAXM = 11000;
35 const double eps = 1e-12;
36 
37 int N, M;
38 int p[MAXN];
39 struct EDGE
40 {
41     int u, v, w;
42 }edge[MAXM];
43 
44 int cmp(EDGE A, EDGE B)
45 {
46     return A.w < B.w;
47 }
48 
49 int findP(int x)
50 {
51     return x == p[x] ? x : p[x] = findP(p[x]);
52 }
53 
54 void Union(int a, int b)
55 {
56     p[findP(a)] = findP(b);
57 }
58 
59 void calc(int s, int t)
60 {
61     int ans = INF;
62     for(int i=0;i<M;i++)
63     {
64         for(int j=0;j<=N;j++) p[j] = j;
65         for(int j=i;j<M;j++)
66         {
67             Union(edge[j].u, edge[j].v);
68             if(findP(s) != findP(t)) continue;
69             ans = MIN(ans, edge[j].w-edge[i].w);
70         }
71     }
72     printf("%d\n", ans == INF ? -1 : ans);
73 }
74 
75 int main()
76 {
77     while(~scanf("%d %d", &N, &M))
78     {
79         int x, y, v;
80         for(int i=0;i<M;i++)
81         {
82             EDGE& e = edge[i];
83             scanf("%d %d %d", &x, &y, &v);
84             e.u = x; e.v = y; e.w = v;
85         }
86         sort(edge, edge + M, cmp);
87         int q, s, t;
88         scanf("%d", &q);
89         while(q--)
90         {
91             scanf("%d %d", &s, &t);
92             calc(s, t);
93         }
94     }
95     return 0;
96 }
View Code
posted @ 2014-07-23 09:21  再见~雨泉  阅读(212)  评论(0编辑  收藏  举报