CF Destroying Roads (最短路)

Destroying Roads
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In some country there are exactly n cities and m bidirectional roads connecting the cities. Cities are numbered with integers from 1 to n. If cities a and b are connected by a road, then in an hour you can go along this road either from city a to city b, or from city b to city a. The road network is such that from any city you can get to any other one by moving along the roads.

You want to destroy the largest possible number of roads in the country so that the remaining roads would allow you to get from city s1 to city t1 in at most l1 hours and get from city s2 to city t2 in at most l2 hours.

Determine what maximum number of roads you need to destroy in order to meet the condition of your plan. If it is impossible to reach the desired result, print -1.

Input

The first line contains two integers nm (1 ≤ n ≤ 3000, ) — the number of cities and roads in the country, respectively.

Next m lines contain the descriptions of the roads as pairs of integers aibi (1 ≤ ai, bi ≤ nai ≠ bi). It is guaranteed that the roads that are given in the description can transport you from any city to any other one. It is guaranteed that each pair of cities has at most one road between them.

The last two lines contains three integers each, s1, t1, l1 and s2, t2, l2, respectively (1 ≤ si, ti ≤ n0 ≤ li ≤ n).

Output

Print a single number — the answer to the problem. If the it is impossible to meet the conditions, print -1.

Sample test(s)
input
5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 2
output
0
input
5 4
1 2
2 3
3 4
4 5
1 3 2
2 4 2
output
1
input
5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 1
output
-1




用dijkstra把每个点都跑一遍,求出任意点之间的最短路,然后先假设两条路之间没重叠,那么可以拆的路就是M - 最短路a - 最短路b,再假设有重叠,枚举他们重叠的段。要注意有可能出现起点和终点要互换的情况。
 1 #include <bits/stdc++.h>
 2 using    namespace    std;
 3 
 4 const    int    INF = 0xfffffff;
 5 const    int    SIZE = 3005;
 6 int    N,M,D[SIZE][SIZE];
 7 int    s_1,t_1,l_1,s_2,t_2,l_2;
 8 bool    S[SIZE];
 9 struct    Q_Node
10 {
11     int    d,vec;
12     bool    operator <(const Q_Node & r)    const
13     {
14         return    d > r.d;
15     };
16 };
17 vector<int>    G[SIZE];
18     
19 void    dijkstra(int);
20 int    main(void)
21 {
22     int    from,to;
23 
24     scanf("%d%d",&N,&M);
25     for(int i = 0;i < M;i ++)
26     {
27         scanf("%d%d",&from,&to);
28         G[from].push_back(to);
29         G[to].push_back(from);
30     }
31     for(int i = 1;i <= N;i ++)
32         dijkstra(i);
33 
34     scanf("%d%d%d",&s_1,&t_1,&l_1);
35     scanf("%d%d%d",&s_2,&t_2,&l_2);
36     if(D[s_1][t_1] > l_1 || D[s_2][t_2] > l_2)
37     {
38         puts("-1");
39         return    0;
40     }
41 
42     int    min = D[s_1][t_1] + D[s_2][t_2];
43     int    ans = 0;
44     for(int i = 1;i <= N;i ++)
45         for(int j = 1;j <= N;j ++)
46         {
47             if(min > D[s_1][i] + D[s_2][i] + D[i][j] + D[j][t_1] + D[j][t_2])
48                 if(D[s_1][i] + D[i][j] + D[j][t_1] <= l_1 && 
49                    D[s_2][i] + D[i][j] + D[j][t_2] <= l_2)
50                     min = D[s_1][i] + D[s_2][i] + D[i][j] + D[j][t_1] + D[j][t_2];
51             if(min > D[t_1][i] + D[t_2][i] + D[i][j] + D[j][s_1] + D[j][s_2])
52                 if(D[t_1][i] + D[i][j] + D[j][s_1] <= l_1 && 
53                    D[t_2][i] + D[i][j] + D[j][s_2] <= l_2)
54                     min = D[t_1][i] + D[t_2][i] + D[i][j] + D[j][s_1] + D[j][s_2];
55             if(min > D[t_1][i] + D[s_2][i] + D[i][j] + D[j][s_1] + D[j][t_2])
56                 if(D[t_1][i] + D[i][j] + D[j][s_1] <= l_1 && 
57                    D[s_2][i] + D[i][j] + D[j][t_2] <= l_2)
58                     min = D[t_1][i] + D[s_2][i] + D[i][j] + D[j][s_1] + D[j][t_2];
59             if(min > D[s_1][i] + D[t_2][i] + D[i][j] + D[j][t_1] + D[j][s_2])
60                 if(D[s_1][i] + D[i][j] + D[j][t_1] <= l_1 && 
61                    D[t_2][i] + D[i][j] + D[j][s_2] <= l_2)
62                     min = D[s_1][i] + D[t_2][i] + D[i][j] + D[j][t_1] + D[j][s_2];
63         }
64     printf("%d\n",M - min);
65 
66     return    0;
67 }
68 
69 void    dijkstra(int s)
70 {
71     priority_queue<Q_Node>    que;
72     Q_Node    temp;
73     for(int i = 0;i <= N;i ++)
74     {
75         D[s][i] = INF;
76         S[i] = false;
77     }
78     D[s][s] = 0;
79     temp.d = 0;
80     temp.vec = s;
81     que.push(temp);
82 
83     while(!que.empty())
84     {
85         int    cur = que.top().vec;
86         que.pop();
87         S[cur] = true;
88 
89         for(int i = 0;i < G[cur].size();i ++)
90             if(!S[G[cur][i]] && D[s][G[cur][i]] > D[s][cur] + 1)
91             {
92                 D[s][G[cur][i]] = D[s][cur] + 1;
93                 temp.vec = G[cur][i];
94                 temp.d = D[s][G[cur][i]];
95                 que.push(temp);
96             }
97     }
98 }

 

posted @ 2015-05-16 17:26  Decouple  阅读(283)  评论(0编辑  收藏  举报