uva 12717 2013Dhaka I

题意:有一张无向图,要求你只能改变变得权值,且边的权值只为1-m每个数只能出现一次。要求最短路树即为最小生成树。

思路:bfs顺序递增的给买一条边赋值,最后再把不在树上的边赋值即为答案。

代码如下:

 1 /**************************************************
 2  * Author     : xiaohao Z
 3  * Blog     : http://www.cnblogs.com/shu-xiaohao/
 4  * Last modified : 2014-07-05 17:22
 5  * Filename     : uva_12717.cpp
 6  * Description     : 
 7  * ************************************************/
 8 
 9 #include <iostream>
10 #include <cstdio>
11 #include <cstring>
12 #include <cstdlib>
13 #include <cmath>
14 #include <algorithm>
15 #include <queue>
16 #include <stack>
17 #include <vector>
18 #include <set>
19 #include <map>
20 #define MP(a, b) make_pair(a, b)
21 #define PB(a) push_back(a)
22 
23 using namespace std;
24 typedef long long ll;
25 typedef pair<int, int> pii;
26 typedef pair<unsigned int,unsigned int> puu;
27 typedef pair<int, double> pid;
28 typedef pair<ll, int> pli;
29 typedef pair<int, ll> pil;
30 
31 const int INF = 0x3f3f3f3f;
32 const double eps = 1E-6;
33 const int LEN = 3010;
34 struct E{
35     int u, v, val;
36 }edge[LEN*10];
37 int n, m, s, top;
38 vector<int> Map[LEN];
39 
40 void bfs(){
41     queue<int> q;
42     int vis[LEN] = {0};
43     vis[s] = 1;
44     q.push(s);
45     while(!q.empty()){
46         int nv = q.front(); q.pop();
47         for(int i=0; i<Map[nv].size(); i++){
48             int pos = Map[nv][i];
49             E &e = edge[pos]; int y = e.u+e.v-nv;
50             if(!vis[y]){
51                 e.val = top++;
52                 vis[y] = 1;
53                 q.push(y);
54             }
55         }
56     }
57 }
58 
59 int main()
60 {
61 //    freopen("in.txt", "r", stdin);
62 
63     ios::sync_with_stdio(false);
64     int T, a, b, c, kase = 1;
65     cin >> T;
66     while(T--){
67         top = 1;
68         cin >> n >> m >> s;
69         for(int i=0; i<LEN; i++) Map[i].clear();
70         for(int i=0; i<m; i++){
71             cin >> a >> b >> c;
72             edge[i].u = a;edge[i].v = b;edge[i].val = -1;
73             Map[a].PB(i);
74             Map[b].PB(i);
75         }
76         bfs();
77         cout << "Case " << kase ++ << ":" << endl;
78         for(int i=0; i<m; i++){
79             if(edge[i].val == -1)edge[i].val = top ++;
80             cout << edge[i].u << ' ' << edge[i].v << ' ' << edge[i].val << endl;
81         }
82     }
83     return 0;
84 }
View Code

 

posted @ 2014-07-05 23:25  张小豪  阅读(440)  评论(0编辑  收藏  举报