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

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 閱讀(391) 評論(0)  編輯 收藏 引用 所屬分類: 圖論

只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            精久久久久久| 久久久精彩视频| 久久久精品tv| 亚洲一级片在线看| 欧美高清成人| 美女日韩在线中文字幕| 国产精品美女在线观看| 欧美va日韩va| 国产毛片久久| 亚洲一区国产精品| 正在播放亚洲一区| 欧美激情视频给我| 欧美成人久久| 国内精品亚洲| 欧美伊人久久久久久久久影院| 中文在线一区| 欧美日韩福利| 亚洲区国产区| 亚洲人成在线观看网站高清| 久久黄金**| 久久精品亚洲| 黄色成人在线网站| 午夜精品视频在线观看一区二区| 亚洲在线中文字幕| 国产精品久久久久久久久搜平片| 日韩视频一区二区三区在线播放| 亚洲免费不卡| 欧美日一区二区在线观看| 亚洲精选一区二区| 亚洲天堂av在线免费观看| 欧美日韩一区自拍| 亚洲天堂免费观看| 欧美中文字幕在线观看| 国产一区二区久久精品| 久久久亚洲国产天美传媒修理工| 久久久在线视频| 亚洲第一精品电影| 欧美aⅴ一区二区三区视频| 亚洲高清资源| 亚洲一区国产视频| 国产麻豆日韩| 久久久精品国产免费观看同学| 裸体丰满少妇做受久久99精品| 在线日韩av片| 欧美日韩卡一卡二| 亚洲在线一区二区三区| 久久蜜臀精品av| 亚洲电影视频在线| 欧美精品v国产精品v日韩精品| 日韩亚洲国产欧美| 欧美一区二区三区四区高清| 国产一区二区成人久久免费影院| 亚洲一区二区三区激情| 国产精品入口麻豆原神| 欧美在线观看视频一区二区三区| 国产精品乱子久久久久| 久久成人免费| 最新国产の精品合集bt伙计| 亚洲一区精彩视频| 悠悠资源网久久精品| 欧美日韩国产成人在线91| 亚洲欧美另类在线| 亚洲第一区在线| 性做久久久久久久久| 亚洲级视频在线观看免费1级| 欧美午夜宅男影院| 久久久一区二区| 中国日韩欧美久久久久久久久| 久久久久久亚洲精品不卡4k岛国| 日韩午夜中文字幕| 激情欧美一区二区| 欧美午夜一区二区| 美国十次了思思久久精品导航| 一区二区三区鲁丝不卡| 欧美v日韩v国产v| 午夜精品久久久久久久男人的天堂| 激情欧美一区二区三区| 国产精品国产福利国产秒拍| 麻豆av一区二区三区| 午夜精品视频在线观看| 日韩视频永久免费| 欧美韩日亚洲| 久久嫩草精品久久久精品| 亚洲午夜国产成人av电影男同| 在线观看三级视频欧美| 国产亚洲第一区| 国产精品久久77777| 欧美精品一区二区视频 | 日韩天堂av| 免费成人黄色| 久久久国产一区二区三区| 亚洲免费视频观看| 一区二区三区鲁丝不卡| 亚洲精品日产精品乱码不卡| 激情文学一区| 国产亚洲视频在线| 国产美女在线精品免费观看| 欧美日韩中文字幕日韩欧美| 欧美国产视频一区二区| 蜜臀va亚洲va欧美va天堂| 久久先锋资源| 久久裸体艺术| 久久久免费精品| 久久精品人人| 久久婷婷国产综合国色天香| 欧美在线视频观看免费网站| 欧美一级播放| 久久精品人人做人人综合| 欧美一区二区精美| 久久高清国产| 久久久久久高潮国产精品视| 久久精品国产77777蜜臀| 久久国产精品久久久久久久久久 | 久久人91精品久久久久久不卡| 欧美影院久久久| 欧美一区二区在线播放| 久久精品1区| 久久亚洲精品一区二区| 男男成人高潮片免费网站| 欧美成人激情视频| 欧美精品一区在线播放| 欧美三区视频| 国产拍揄自揄精品视频麻豆| 国产一区二区日韩精品欧美精品| 国产亚洲精品资源在线26u| 激情av一区| 99精品欧美一区二区蜜桃免费| 一本大道久久a久久精品综合| 亚洲视频电影图片偷拍一区| 亚洲欧美一区二区激情| 久久久午夜精品| 亚洲电影免费观看高清完整版| 亚洲国产欧美日韩精品| 国产精品99久久久久久久久| 香蕉av777xxx色综合一区| 久久久久88色偷偷免费| 欧美极品在线播放| 国产精品欧美一区二区三区奶水| 国产一区二区三区无遮挡| 亚洲人成网站色ww在线| 午夜精品久久久久影视 | 久久手机精品视频| 欧美精品系列| 国产乱人伦精品一区二区 | 亚洲国产精品激情在线观看| 99re在线精品| 久久国产福利国产秒拍| 欧美激情一区二区三区四区| 99re视频这里只有精品| 欧美一区二区三区四区高清| 欧美福利在线| 国产一区二区三区直播精品电影| 亚洲精品美女91| 久久精彩免费视频| 亚洲精品国产精品国自产观看| 翔田千里一区二区| 欧美日韩国产bt| 悠悠资源网久久精品| 亚洲免费在线播放| 亚洲第一主播视频| 欧美一区二区| 国产精品国产三级欧美二区| 亚洲黄一区二区| 久久爱www| 亚洲深夜av| 欧美激情精品久久久| 韩国av一区二区三区| 亚洲午夜精品在线| 亚洲国产婷婷综合在线精品 | 欧美精品色综合| 1024成人| 久久精品免费电影| 亚洲男人的天堂在线aⅴ视频| 欧美另类久久久品| 亚洲国产欧美在线人成| 久久久久久久久岛国免费| 亚洲午夜在线观看视频在线| 欧美另类极品videosbest最新版本| 狠狠色狠狠色综合人人| 午夜精品视频一区| 9l国产精品久久久久麻豆| 欧美国产成人精品| 亚洲人成人77777线观看| 蜜桃久久av| 久久精品二区| 国内自拍视频一区二区三区| 欧美一区二区性| 亚洲欧美日韩精品综合在线观看| 欧美日韩在线精品| 亚洲天堂视频在线观看| 一本大道av伊人久久综合| 欧美另类在线播放| 99这里只有久久精品视频| 亚洲精品乱码久久久久久蜜桃91| 欧美韩日精品| 夜夜嗨av一区二区三区免费区| 最新日韩av| 欧美日韩三级在线| 亚洲午夜视频在线| 亚洲性图久久|