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

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 閱讀(390) 評論(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>
            国产精品久久九九| 国产精品日韩久久久| 精品成人在线| 美女91精品| 老牛影视一区二区三区| 亚洲国产精品成人精品| 亚洲国产女人aaa毛片在线| 蜜臀久久99精品久久久画质超高清 | 久久综合九色综合欧美就去吻| 性色av一区二区三区红粉影视| 国产一区二区三区四区在线观看| 久久天堂成人| 女同性一区二区三区人了人一| 日韩午夜激情av| 中日韩高清电影网| 国产一区自拍视频| 欧美激情在线| 国产精品成人aaaaa网站| 校园春色综合网| 久久精品国产精品亚洲| 亚洲精品永久免费| 一区二区三区欧美| 在线播放精品| 亚洲色图在线视频| 尤物精品在线| 一区二区三区精品| 亚洲国产精品va在线看黑人| 日韩午夜av电影| 国产一区二区三区黄| 亚洲黄色免费电影| 国产日韩在线一区二区三区| 欧美激情国产精品| 国产欧美精品| 亚洲精品国精品久久99热一| 国产一区二区三区高清| 亚洲另类一区二区| 伊人久久久大香线蕉综合直播 | 亚洲女人天堂av| 另类欧美日韩国产在线| 午夜精品亚洲一区二区三区嫩草| 久久尤物视频| 久久久久青草大香线综合精品| 欧美精品成人91久久久久久久| 久久精品成人一区二区三区蜜臀 | 欧美一区二区播放| 亚洲一本视频| 欧美成年人在线观看| 久久精品99| 国产精品自在欧美一区| 99国内精品久久| 亚洲片在线观看| 鲁大师成人一区二区三区| 欧美一区二区视频在线观看2020| 欧美欧美全黄| 91久久精品www人人做人人爽| 亚洲承认在线| 久久午夜色播影院免费高清| 久久精品综合一区| 国产美女高潮久久白浆| 亚洲男人第一网站| 午夜国产精品视频| 国产精品久久久久久久久久久久久久| 亚洲国产精品尤物yw在线观看| 黄色一区二区三区四区| 久久精品在线免费观看| 久久久久国产精品人| 国产在线观看91精品一区| 亚洲综合精品自拍| 欧美一级大片在线观看| 国产精品黄色| 亚洲欧美日韩一区| 欧美在线视频不卡| 国产欧美一区二区精品仙草咪 | 老妇喷水一区二区三区| 国模一区二区三区| 久久久久九九九| 欧美黄色成人网| 亚洲日本理论电影| 欧美日韩国产大片| 一区二区高清在线| 久久aⅴ国产紧身牛仔裤| 国产日韩欧美日韩| 久久夜色精品国产亚洲aⅴ | 久久婷婷激情| 国产一区91| 蜜桃av久久久亚洲精品| 亚洲黄色高清| 欧美一级久久久| 一区二区三区亚洲| 欧美激情精品久久久久久久变态 | 久久尤物视频| 亚洲毛片av在线| 亚洲欧美视频在线观看| 国产一区二区三区无遮挡| 久久午夜精品一区二区| 亚洲免费不卡| 久久久女女女女999久久| 亚洲人精品午夜| 国产农村妇女毛片精品久久莱园子| 欧美在线一区二区三区| 亚洲国产日韩欧美在线图片| 亚洲伊人观看| 亚洲国产精品激情在线观看| 欧美日韩日日骚| 久久婷婷激情| 亚洲伊人一本大道中文字幕| 男女视频一区二区| 亚洲欧美精品一区| 亚洲日本成人在线观看| 国产亚洲福利社区一区| 欧美日韩大片| 久久久久一区二区三区| 一区二区三区日韩精品视频| 免费成人在线观看视频| 午夜精品久久久久影视| 亚洲激情视频在线播放| 国产欧美精品日韩| 欧美巨乳波霸| 久久亚洲国产精品一区二区 | 亚洲色图综合久久| 欧美激情二区三区| 久久三级福利| 午夜精品一区二区三区在线播放| 亚洲人成人99网站| 激情欧美国产欧美| 国产精品一区二区a| 欧美日韩国产限制| 欧美阿v一级看视频| 久久国产精品72免费观看| 亚洲午夜未删减在线观看| 最新国产成人av网站网址麻豆| 久久一区亚洲| 久久国产婷婷国产香蕉| 午夜欧美大尺度福利影院在线看| 亚洲精品久久| 亚洲欧洲日产国产综合网| 好看不卡的中文字幕| 国产日韩欧美91| 国产模特精品视频久久久久| 欧美日韩在线三级| 欧美日韩1区| 欧美国产日韩一区二区三区| 久久视频精品在线| 久久一区二区三区av| 久久午夜精品| 免费观看成人| 欧美成黄导航| 欧美伦理91i| 欧美久久久久久久| 欧美日韩午夜剧场| 国产精品电影在线观看| 国产精品卡一卡二卡三| 国产精品美女久久福利网站| 国产精品久久77777| 国产精品视频福利| 国产精品一卡二卡| 韩国女主播一区二区三区| 激情视频一区二区| 91久久国产自产拍夜夜嗨| 亚洲精品日日夜夜| 亚洲综合第一页| 欧美在线日韩精品| 欧美高清视频| 亚洲精品综合久久中文字幕| 一区二区三区国产盗摄| 亚洲欧美一区二区三区极速播放| 久久国产精品黑丝| 欧美国产高清| 国产精品人人爽人人做我的可爱| 国产欧美日韩另类一区| 极品av少妇一区二区| 亚洲免费黄色| 欧美在线日韩精品| 欧美电影打屁股sp| 99精品热视频只有精品10| 亚洲自拍偷拍麻豆| 另类天堂av| 国产精品夜夜嗨| 91久久在线视频| 午夜一区不卡| 欧美激情国产日韩精品一区18| 亚洲深夜av| 男女激情久久| 国产欧美日韩一区| 亚洲裸体视频| 久久青草欧美一区二区三区| 亚洲伦理中文字幕| 久久久精彩视频| 国产精品久久999| 亚洲青色在线| 久久久午夜视频| 亚洲精品一线二线三线无人区| 欧美一级久久久久久久大片| 欧美精品日韩三级| 一区二区三区亚洲| 亚洲欧美一区二区视频| 亚洲国产视频直播| 久久精品一区二区三区中文字幕| 欧美视频一区二区三区| 亚洲欧洲日韩女同|