• POJ 1679 The Unique MST


    The Unique MST
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 19847   Accepted: 6959

    Description

    Given a connected undirected graph, tell if its minimum spanning tree is unique. 

    Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties: 
    1. V' = V. 
    2. T is connected and acyclic. 

    Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'. 

    Input

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <queue>
     5 #define INF 0x3f3f3f3f
     6 #define pii pair<int,int>
     7 using namespace std;
     8 const int maxn = 10000;
     9 struct arc {
    10     int to,cost,next;
    11     arc(int x = 0,int y = 0,int z = -1) {
    12         to = x;
    13         cost = y;
    14         next = z;
    15     }
    16 };
    17 arc e[maxn];
    18 int head[maxn],d[maxn],p[maxn],n,m,tot;
    19 bool used[maxn];
    20 void add(int u,int v,int w) {
    21     e[tot] = arc(v,w,head[u]);
    22     head[u] = tot++;
    23 }
    24 int prim() {
    25     priority_queue<pii,vector< pii >,greater< pii > >q;
    26     for(int i = 1; i < maxn; ++i) {
    27         d[i] = INF;
    28         used[i] = false;
    29         p[i] = -1;
    30     }
    31     d[1] = 0;
    32     q.push(make_pair(d[1],1));
    33     int ans = 0;
    34     bool isUnique = true;
    35     while(!q.empty()) {
    36         pair<int,int>now = q.top();
    37         q.pop();
    38         if(used[now.second]) continue;
    39         used[now.second] = true;
    40         ans += now.first;
    41         for(int i = head[now.second]; ~i; i = e[i].next) {
    42             if(!used[e[i].to] && d[e[i].to] > e[i].cost) {
    43                 d[e[i].to] = e[i].cost;
    44                 p[e[i].to] = now.second;
    45                 q.push(make_pair(d[e[i].to],e[i].to));
    46             }
    47             if(used[e[i].to] && p[now.second] != -1 && p[now.second] != e[i].to && e[i].cost == now.first) isUnique = false;
    48         }
    49     }
    50     return isUnique?ans:-1;
    51 }
    52 int main() {
    53     int kase;
    54     scanf("%d",&kase);
    55     while(kase--) {
    56         scanf("%d %d",&n,&m);
    57         memset(head,-1,sizeof(head));
    58         for(int i = tot = 0; i < m; ++i) {
    59             int u,v,w;
    60             scanf("%d %d %d",&u,&v,&w);
    61             add(u,v,w);
    62             add(v,u,w);
    63         }
    64         int ans = prim();
    65         ans == -1?puts("Not Unique!"):printf("%d
    ",ans);
    66     }
    67     return 0;
    68 }
    69 /*
    70 2
    71 3 3
    72 1 2 1
    73 2 3 2
    74 3 1 3
    75 4 4
    76 1 2 2
    77 2 3 2
    78 3 4 2
    79 4 1 2
    80 
    81 */
    View Code

     

     

    The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.

    Output

    For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.

    Sample Input

    2
    3 3
    1 2 1
    2 3 2
    3 1 3
    4 4
    1 2 2
    2 3 2
    3 4 2
    4 1 2
    

    Sample Output

    3
    Not Unique!
    

    Source

     
    解题:测试数据好像有问题啊,大大的问题。。。下面写法貌似只能过这题
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <vector>
     6 #include <climits>
     7 #include <algorithm>
     8 #include <cmath>
     9 #define LL long long
    10 #define INF 0x3f3f3f
    11 using namespace std;
    12 int mp[101][101],d[101];
    13 int n,m;
    14 bool vis[101];
    15 int prim(){
    16     int i,j,k,temp,index,ans = 0;
    17     for(i = 1; i <= n; i++) d[i] = INF;
    18     d[1] = 0;
    19     memset(vis,false,sizeof(vis));
    20     for(i = 0; i < n; i++){
    21         temp = INF;
    22         for(j = 1; j <= n; j++)
    23             if(!vis[j] && d[j] < temp) temp = d[index = j];
    24         k = 0;
    25         for(j = 1; j <= n; j++){
    26             if(vis[j] && mp[index][j] == temp) k++;
    27         }
    28         if(k > 1) return -1;
    29         vis[index] = true;
    30         ans += temp;
    31         for(j = 1; j <= n; j++){
    32             if(!vis[j] && d[j] > mp[index][j]) d[j] = mp[index][j];
    33         }
    34     }
    35     return ans;
    36 }
    37 int main(){
    38     int ks,i,j,u,v,w;
    39     scanf("%d",&ks);
    40     while(ks--){
    41         scanf("%d%d",&n,&m);
    42         for(i = 0; i <= n; i++)
    43             for(j = 0; j <= n; j++)
    44             mp[i][j] = INF;
    45         for(i = 0; i < m; i++){
    46             scanf("%d%d%d",&u,&v,&w);
    47             if(mp[u][v] > w){
    48                 mp[u][v] = mp[v][u] = w;
    49             }
    50         }
    51         w = prim();
    52         if(w < 0) puts("Not Unique!");
    53         else printf("%d
    ",w);
    54     }
    55     return 0;
    56 }
    View Code

    上面的代码有些许问题

    完整的正确代码

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int maxn = 110;
    18 int g[maxn][maxn],d[maxn],p[maxn],maxv[maxn][maxn],used[maxn][maxn],n,m;
    19 bool vis[maxn];
    20 int prim() {
    21     for(int i = 1; i <= n; ++i) {
    22         d[i] = INF;
    23         p[i] = -1;
    24         vis[i] = false;
    25     }
    26     int sum = d[1] = 0;
    27     for(int i = 1; i <= n; ++i) {
    28         int minv = INF,index;
    29         for(int j = 1; j <= n; ++j)
    30             if(!vis[j] && d[j] < minv)
    31                 minv = d[index = j];
    32         if(p[index] > -1) {
    33             used[index][p[index]] = used[p[index]][index] = 2;
    34             for(int j = 1; j <= n; ++j)
    35                 if(vis[j]) maxv[j][index] = maxv[index][j] = max(maxv[j][p[index]],minv);
    36         }
    37         sum += minv;
    38         vis[index] = true;
    39         for(int j = 1; j <= n; ++j)
    40             if(!vis[j] && g[index][j] < INF && g[index][j] < d[j]) {
    41                 d[j] = g[index][j];
    42                 p[j] = index;
    43             }
    44     }
    45     return sum;
    46 }
    47 int main() {
    48     int t,u,v,w;
    49     scanf("%d",&t);
    50     while(t--) {
    51         scanf("%d %d",&n,&m);
    52         for(int i = 0; i <= n; ++i)
    53             for(int j = 0; j <= n; ++j) {
    54                 maxv[i][j] = 0;
    55                 g[i][j] = INF;
    56                 used[i][j] = 0;
    57             }
    58         for(int i = 0; i < m; ++i){
    59             scanf("%d %d %d",&u,&v,&w);
    60             if(g[u][v] > w){
    61                 g[u][v] = g[v][u] = w;
    62                 used[u][v] = used[v][u] = 1;
    63             }
    64         }
    65         int ans = prim(),ans2 = INF;
    66         for(int i = 1; i <= n; ++i)
    67         for(int j = i + 1; j <= n; ++j){
    68             if(used[i][j] == 1) ans2 = min(ans2,ans - maxv[i][j] + g[i][j]);
    69         }
    70         ans == ans2?puts("Not Unique!"):printf("%d
    ",ans);
    71     }
    72     return 0;
    73 }
    View Code

    不用求次小生成树,也能判断是否唯一,prim版

  • 相关阅读:
    关于 CommonJS AMD CMD UMD 规范
    如何成为一名卓越的前端工程师
    javascript 中 void 0的含义及undefine于void 0区别
    原生js获取样式表:currentStyle与defaultView的区别 真实例子
    attachEvent与addEventListener的区别 真实例子
    将图片转换成黑白(灰色、置灰)
    前端图片缓存问题
    html里的<wbr>标签什么意思
    关于SQL SERVER中的FLOAT转换为VARCHAR
    记一次工作需求: RSA密钥之C#格式与Java格式转换
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3856991.html
Copyright © 2020-2023  润新知