青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

posts - 7,comments - 3,trackbacks - 0
Slim Span
Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 4023Accepted: 2116

Description

Given an undirected weighted graph G, you should find one of spanning trees specified as follows.

The graph G is an ordered pair (VE), where V is a set of vertices {v1v2, …, vn} and E is a set of undirected edges {e1e2, …, em}. Each edge e ∈ E has its weight w(e).

A spanning tree T is a tree (a connected subgraph without cycles) which connects all the n vertices with n − 1 edges. The slimness of a spanning tree T is defined as the difference between the largest weight and the smallest weight among the n − 1 edges of T.


Figure 5: A graph G and the weights of the edges

For example, a graph G in Figure 5(a) has four vertices {v1v2v3v4} and five undirected edges {e1e2e3e4e5}. The weights of the edges are w(e1) = 3, w(e2) = 5, w(e3) = 6, w(e4) = 6, w(e5) = 7 as shown in Figure 5(b).


Figure 6: Examples of the spanning trees of G

There are several spanning trees for G. Four of them are depicted in Figure 6(a)~(d). The spanning tree Ta in Figure 6(a) has three edges whose weights are 3, 6 and 7. The largest weight is 7 and the smallest weight is 3 so that the slimness of the tree Ta is 4. The slimnesses of spanning trees TbTc and Td shown in Figure 6(b), (c) and (d) are 3, 2 and 1, respectively. You can easily see the slimness of any other spanning tree is greater than or equal to 1, thus the spanning tree Td in Figure 6(d) is one of the slimmest spanning trees whose slimness is 1.

Your job is to write a program that computes the smallest slimness.

Input

The input consists of multiple datasets, followed by a line containing two zeros separated by a space. Each dataset has the following format.

nm
a1b1w1
ambmwm

Every input item in a dataset is a non-negative integer. Items in a line are separated by a space. n is the number of the vertices and m the number of the edges. You can assume 2 ≤ n ≤ 100 and 0 ≤ m ≤ n(n − 1)/2. ak and bk (k = 1, …, m) are positive integers less than or equal to n, which represent the two vertices vak and vbk connected by the kth edge ekwk is a positive integer less than or equal to 10000, which indicates the weight of ek. You can assume that the graph G = (VE) is simple, that is, there are no self-loops (that connect the same vertex) nor parallel edges (that are two or more edges whose both ends are the same two vertices).

Output

For each dataset, if the graph has spanning trees, the smallest slimness among them should be printed. Otherwise, −1 should be printed. An output should not contain extra characters.

Sample Input

4 5
1 2 3
1 3 5
1 4 6
2 4 6
3 4 7
4 6
1 2 10
1 3 100
1 4 90
2 3 20
2 4 80
3 4 40
2 1
1 2 1
3 0
3 1
1 2 1
3 3
1 2 2
2 3 5
1 3 6
5 10
1 2 110
1 3 120
1 4 130
1 5 120
2 3 110
2 4 120
2 5 130
3 4 120
3 5 110
4 5 120
5 10
1 2 9384
1 3 887
1 4 2778
1 5 6916
2 3 7794
2 4 8336
2 5 5387
3 4 493
3 5 6650
4 5 1422
5 8
1 2 1
2 3 100
3 4 100
4 5 100
1 5 50
2 5 50
3 5 50
4 1 150
0 0

Sample Output

1
20
0
-1
-1
1
0
1686
50

Source



題目就是生成一棵樹,要求邊權最大減最小的差最小。
根據Kruskal思想,把邊排序,之后枚舉一下就行了。

代碼:

#include <cmath>
#include 
<cstdio>
#include 
<cstdlib>
#include 
<cstring>
#include 
<iostream>
#include 
<algorithm>
using namespace std;

const int M = 5005;
const int INF = 1 << 29;

struct edge
{
    
int st, ed, w;
    
bool operator < (edge a) const
    {
        
return w < a.w;
    }
} e[M];

int n, m, ans, num, temp;
int f[105], rank[105];

void makeset()
{
    
for (int i = 1; i <= n; ++i)
        f[i] 
= i;
    memset(rank, 
0sizeof(rank));
}

int find(int x)
{
    
while (f[x] != x) x = f[x];
    
return x;
}

void unionset(int a, int b)
{
    
int p = find(a);
    
int q = find(b);
    
if (rank[p] > rank[q])
        f[q] 
= p;
    
else
    
if (rank[p] < rank[q])
        f[p] 
= q;
    
else
    {
        f[p] 
= q;
        rank[q]
++;
    }
}

void kruskal()
{
    ans 
= INF;
    
for (int i = 0; i < m - n + 2++i)
    {
        makeset();
        temp 
= -1;
        num 
= 0;
        
for (int j = i; j < m; ++j)
        {
            
if (find(e[j].st) != find(e[j].ed))
            {
                num
++;
                unionset(e[j].st, e[j].ed);
                
if (num == n - 1)
                {
                    temp 
= e[j].w - e[i].w;
                    
break;
                }
            }
        }
        
if (temp == -1break;
        
if (temp != -1 && temp < ans) ans = temp;
    }
    
if (ans >= INF) printf("-1\n");
    
else printf("%d\n", ans);
}

int main()
{
    
while (scanf("%d%d"&n, &m), n || m)
    {
        
for (int i = 0; i < m; ++i)
            scanf(
"%d%d%d"&e[i].st, &e[i].ed, &e[i].w);
        sort(e, e 
+ m);
        kruskal();
    }
    
return 0;
}
posted on 2011-10-17 15:54 LLawliet 閱讀(381) 評論(0)  編輯 收藏 引用 所屬分類: 圖論
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            国产精品青草久久| 欧美精品成人91久久久久久久| 日韩视频精品在线观看| 亚洲国产毛片完整版| 久久精品国产久精国产一老狼| 欧美日韩在线电影| 亚洲欧美在线aaa| 蜜臀va亚洲va欧美va天堂| 99视频超级精品| 久久精品二区亚洲w码| 亚洲精品五月天| 亚洲欧美日韩直播| 日韩一级大片| 久久影院亚洲| 久久精品国产视频| 国产精品黄色| 日韩视频欧美视频| 亚洲精品国产精品国自产观看浪潮 | 久久精品91久久久久久再现| 日韩午夜av在线| 亚洲精品视频免费观看| 久久综合久久久久88| 欧美日韩一区二| 中日韩午夜理伦电影免费| 亚洲女优在线| 欧美激情一区二区三区在线视频| 欧美一区二区三区日韩视频| 国产欧美一二三区| 午夜精品福利一区二区蜜股av| 欧美大色视频| 亚洲日本成人在线观看| 欧美激情视频一区二区三区免费 | 国产精品美女久久久浪潮软件 | 久久久精品网| 欧美一级黄色网| 国产亚洲欧美一区二区三区| 亚洲宅男天堂在线观看无病毒| 亚洲私人黄色宅男| 国产精品视频免费一区| 亚洲欧美怡红院| 欧美www视频| 中文日韩在线视频| 国产精品中文字幕欧美| 久久久久久综合| 亚洲欧洲日产国产网站| 午夜精品久久久久久久久久久久久| 国产精品99一区二区| 欧美有码在线视频| 亚洲国产精品久久人人爱蜜臀| 亚洲深夜影院| 在线播放日韩专区| 国产啪精品视频| 蜜桃精品一区二区三区 | 99国内精品久久| 久久久免费精品视频| 一区二区欧美精品| 在线观看欧美成人| 国内精品一区二区三区| 国产精品福利片| 欧美日韩综合视频网址| 免费观看在线综合色| 久久免费高清视频| 久久久久久久久久看片| 亚洲欧美日本国产有色| 亚洲精品视频中文字幕| 亚洲国产经典视频| 欧美电影免费观看| 欧美国产第二页| 亚洲高清不卡av| 亚洲狠狠婷婷| 亚洲人成网站在线观看播放| 亚洲国产99| 亚洲国产一区在线| 亚洲精品在线一区二区| 亚洲免费播放| 中日韩午夜理伦电影免费| 在线午夜精品自拍| 午夜免费在线观看精品视频| 欧美在线不卡| 欧美久久久久免费| 国产精品尤物福利片在线观看| 国产精品一区二区三区久久久| 国产日韩精品久久久| 欧美国产专区| 在线一区亚洲| 久久一区中文字幕| 欧美精品二区| 国产精品theporn| 一色屋精品视频免费看| 夜夜爽99久久国产综合精品女不卡| 亚洲一区二区三区乱码aⅴ| 久久久亚洲精品一区二区三区| 亚洲激情一区| 欧美一级久久久| 国产精品日韩欧美一区二区三区 | 一区二区三区免费观看| 国产精品一区二区三区四区五区| 国产精品一区二区在线观看网站| 欧美日韩国产精品专区| 黄色免费成人| 久久九九国产精品怡红院| 亚洲福利视频二区| 噜噜噜91成人网| 在线观看欧美精品| 欧美韩日精品| 欧美成人黄色小视频| 亚洲国产精品999| 亚洲第一在线综合在线| 免费视频久久| 一区二区三区欧美在线观看| 亚洲精品久久嫩草网站秘色| 欧美日本免费一区二区三区| 亚洲美女黄色| 一区二区三区四区五区精品| 欧美日韩免费| 久久精品视频导航| 蜜桃av综合| 亚洲一区二区高清| 欧美一区二区视频观看视频| 韩国女主播一区| 亚洲国产欧美精品| 国产精品一区二区三区四区五区| 欧美在线网址| 欧美精品久久天天躁| 午夜精品一区二区三区四区| 久久久福利视频| 在线亚洲+欧美+日本专区| 久久嫩草精品久久久精品一| 中国成人亚色综合网站| 欧美在线观看一二区| 中文日韩电影网站| 另类激情亚洲| 久久亚洲精品一区二区| 国产精品视频精品视频| 亚洲人成在线观看一区二区| 精品二区久久| 性色av一区二区三区红粉影视| 一卡二卡3卡四卡高清精品视频| 午夜精彩国产免费不卡不顿大片| 91久久精品国产91久久| 久久精品国产99国产精品澳门| 99国产精品久久久久老师| 久久久久久亚洲精品中文字幕| 亚洲自拍偷拍视频| 欧美视频在线观看免费网址| 亚洲国产综合在线看不卡| 亚洲第一页自拍| 欧美jizz19性欧美| 免费看av成人| 一区二区激情| 国产精品男女猛烈高潮激情| 一区二区三区视频免费在线观看| 亚洲最新视频在线播放| 国产精品h在线观看| 一区二区三区|亚洲午夜| 羞羞答答国产精品www一本| 国产精品色婷婷久久58| 欧美制服第一页| 亚洲国产精品久久久久秋霞蜜臀 | 噜噜噜久久亚洲精品国产品小说| 老牛国产精品一区的观看方式| 精品动漫3d一区二区三区免费版| 久久久久一区二区三区| 一区二区欧美视频| 久久综合影音| 欧美一级夜夜爽| 一区二区三区在线视频观看| 欧美精品在线极品| 欧美在线播放高清精品| 一区二区欧美在线观看| 欧美成人r级一区二区三区| 性亚洲最疯狂xxxx高清| 亚洲欧洲精品一区二区三区| 国产乱码精品1区2区3区| 欧美人与性动交α欧美精品济南到| 亚洲中字在线| 亚洲特级片在线| 日韩视频免费在线观看| 亚洲国产精品成人综合色在线婷婷| 欧美一区午夜精品| 亚洲欧美中文字幕| 亚洲无玛一区| 亚洲一区二区三区四区在线观看| 亚洲国产精品久久人人爱蜜臀| 国外成人网址| 精品成人在线观看| 亚洲电影免费在线 | 久久综合五月| 玖玖综合伊人| 欧美成人69av| 亚洲免费观看高清在线观看| 91久久国产精品91久久性色| 亚洲国产精品专区久久| 亚洲精品欧洲| 欧美一级片久久久久久久| 久久久999精品免费| 欧美成人午夜激情视频| 国产精品v日韩精品v欧美精品网站| 国产精品久久久久久久久久久久久| 国产精品一级|