锘??xml version="1.0" encoding="utf-8" standalone="yes"?>久久久久亚洲AV无码去区首,国产高潮国产高潮久久久91 ,久久亚洲国产最新网站http://www.shnenglu.com/lvlawliet/category/17933.htmlVIMzh-cnMon, 17 Oct 2011 19:01:49 GMTMon, 17 Oct 2011 19:01:49 GMT60POJ1679:The Unique MSThttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158577.htmlLLawlietLLawlietMon, 17 Oct 2011 13:07:00 GMThttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158577.htmlhttp://www.shnenglu.com/lvlawliet/comments/158577.htmlhttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158577.html#Feedback0http://www.shnenglu.com/lvlawliet/comments/commentRss/158577.htmlhttp://www.shnenglu.com/lvlawliet/services/trackbacks/158577.html
The Unique MST
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 11943
Accepted: 4112

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

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

POJ Monthly--2004.06.27 srbga@P

浠g爜錛?br />
#include <iostream>
#include 
<cstdio>
#include 
<cstring>
#include 
<algorithm>
using namespace std;

typedef 
struct
{
    
int x, y, w;
} edge;

edge s[
10010];
int top, t[10010];

bool cmp(edge a, edge b)
{
    
return a.w < b.w;
}

int kru(int n, int m, int x)
{
    
int i, j, a, b, tag[110], tem, sum, k;
    
for (i = 0; i < n; ++i)
    {
        tag[i] 
= i;
    }
    k 
= 1, j = 0, sum = 0;
    
while (k < n)
    {
        a 
= s[j].x - 1;
        b 
= s[j].y - 1;
        
if (j == x)
        {
            j
++;
            
continue;
        }
        
if (tag[a] != tag[b])
        {
            
if (x == -1)
            {
                t[top] 
= j;
                top
++;
            }
            tem 
= tag[b];
            k
++, sum += s[j].w;
            
for (i =  0; i < n; ++i)
            {
                
if (tag[i] == tem)
                {
                    tag[i] 
= tag[a];
                }
            }
        }
        j
++;
    }
    
return sum;
}
int main()
{
    
int p, n, m, cmin;
    scanf(
"%d"&p);
    
while (p--)
    {
        
int flag = 1;
        scanf(
"%d%d"&n, &m);
        
for (int i = 0; i < m; ++i)
            scanf(
"%d%d%d"&s[i].x, &s[i].y, &s[i].w);
        sort(s, s 
+ m, cmp);
        top 
= 0;
        
int min = kru(n, m, -1);
        
int key = top;
        
for (int l = 0; l < key; ++l)
        {
            cmin 
= kru(n, m, t[l]);
            
if (cmin == min)
            {
                flag 
= 0;
                printf(
"Not Unique!\n");
                
break;
            }
        }
        
if (flag)
        printf(
"%d\n", min);
    }
    
return 0;
}


LLawliet 2011-10-17 21:07 鍙戣〃璇勮
]]>
POJ3463錛歋ightseeinghttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158556.htmlLLawlietLLawlietMon, 17 Oct 2011 08:30:00 GMThttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158556.htmlhttp://www.shnenglu.com/lvlawliet/comments/158556.htmlhttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158556.html#Feedback0http://www.shnenglu.com/lvlawliet/comments/commentRss/158556.htmlhttp://www.shnenglu.com/lvlawliet/services/trackbacks/158556.html
Sightseeing
Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 4917Accepted: 1688

Description

Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one city S to another city F. On this way, the tourists in the bus can see the sights alongside the route travelled. Moreover, the bus makes a number of stops (zero or more) at some beautiful cities, where the tourists get out to see the local sights.

Different groups of tourists may have different preferences for the sights they want to see, and thus for the route to be taken from S to F. Therefore, Your Personal Holiday wants to offer its clients a choice from many different routes. As hotels have been booked in advance, the starting city S and the final city F, though, are fixed. Two routes from S to F are considered different if there is at least one road from a city A to a city B which is part of one route, but not of the other route.

There is a restriction on the routes that the tourists may choose from. To leave enough time for the sightseeing at the stops (and to avoid using too much fuel), the bus has to take a short route from S to F. It has to be either a route with minimal distance, or a route which is one distance unit longer than the minimal distance. Indeed, by allowing routes that are one distance unit longer, the tourists may have more choice than by restricting them to exactly the minimal routes. This enhances the impression of a personal holiday.

For example, for the above road map, there are two minimal routes from S = 1 to F = 5: 1 → 2 → 5 and 1 → 3 → 5, both of length 6. There is one route that is one distance unit longer: 1 → 3 → 4 → 5, of length 7.

Now, given a (partial) road map of the Benelux and two cities S and F, tour operator Your Personal Holiday likes to know how many different routes it can offer to its clients, under the above restriction on the route length.

Input

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

  • One line with two integers N and M, separated by a single space, with 2 ≤ N ≤ 1,000 and 1 ≤ M ≤ 10, 000: the number of cities and the number of roads in the road map.

  • M lines, each with three integers AB and L, separated by single spaces, with 1 ≤ AB ≤ NA ≠ B and 1 ≤ L ≤ 1,000, describing a road from city A to city B with length L.

    The roads are unidirectional. Hence, if there is a road from A to B, then there is not necessarily also a road from B to A. There may be different roads from a city A to a city B.

  • One line with two integers S and F, separated by a single space, with 1 ≤ SF ≤ N and S ≠ F: the starting city and the final city of the route.

    There will be at least one route from S to F.

Output

For every test case in the input file, the output should contain a single number, on a single line: the number of routes of minimal length or one distance unit longer. Test cases are such, that this number is at most 109 = 1,000,000,000.

Sample Input

2
5 8
1 2 3
1 3 2
1 4 5
2 3 1
2 5 3
3 4 2
3 5 4
4 5 3
1 5
5 6
2 3 1
3 2 1
3 1 10
4 5 2
5 2 7
5 2 7
4 1

Sample Output

3
2

Hint

The first test case above corresponds to the picture in the problem description.

Source



鎬濊礬:
渚濇嵁鎻忓啓鍙煡錛屾湰棰樼殑瑕佹眰鍗充究瑕佹眰鍑烘渶鐭礬鍜屾瘮鏈鐭礬闀?鐨勬鐭礬錛屽洜鑰屽彲鐢―ijkstra鏉ュ鐞嗐傜繑瀹炲仛娉曞涓嬶細鐢ㄤ袱緇勬暟紱誨埆鐧昏鏈鐭礬鍜屾鐭礬鐨勯暱搴︼紙dist錛夛紝鏉℃暟錛坈nt錛夛紝鎷滀細絎﹀彿錛坲sed錛夛紝寤轟竴涓紭鍏堥槦鍒楋紝鍏冪礌鍗曚綅鍖呮嫭鑺傜偣搴忓彿錛坴錛夛紝璇ヨ妭鐐硅礬緇忛暱錛坙en錛夛紝浠ュ強鐧昏璺緞縐嶇被錛坮ef錛夛紝姣忔浠庝紭鍏堥槦鍒椾腑鍙栧嚭綆$敤鑺傜偣鍚庯紝鐢ㄥ畠鎵鐧昏鐨勮礬寰勯暱鏇存柊寰呮瘮鎷熻礬寰勶紝紱誨埆鐢ㄥ畠鍜岀洰鍓嶆墍鐧昏鐨勮鑺傜偣鐨勬渶鐭礬寰勪互鍙婃孌佃礬寰勬瘮鎷燂紝涓剰鏇存柊鏉′歡鍒欑櫥璁拌礬寰勭綾伙紝騫剁敓鎴愭柊鑺傜偣鍔犲叆浼樺厛闃熷垪錛屽悓鏃舵洿鏂扮洰鍓嶈妭鐐瑰璇ョ綾昏礬寰勬潯鏁般備竾涓涓嶄腑鎰忔潯浠剁劧鑰屼腑鎰忔販鍚岃仈緋伙紝鍒欏鍔犵浉搴旂殑鏉℃暟鍒拌鑺傜偣鎵鐧昏鐨勮礬寰勬潯鏁頒笂銆?/span>

浠g爜錛?br />
#include <cstdio>
#include 
<memory.h>
#include 
<queue>
#define N 1001
#define M 10001
#define INF 0x7fffffff
#define clr(a) memset(a, 0, sizeof(a))
using namespace std;

struct Edge
{
    
int v, len, ref;
    Edge 
*link;
    Edge new_E(
int v1, int l, int r)
    {
        v 
= v1, len = l, ref = r;
        
return *this;
    }
*E[N], mempool[M];

int dist[N][2], used[N][2], cnt[N][2];
int n, m, memh, S, T;

void AddEdge(int u, int v, int len)
{
    Edge 
*= &mempool[memh++];
    e 
-> v = v;
    e 
-> len = len;
    e 
-> link = E[u];
    E[u] 
= e;
}

bool operator < (Edge a, Edge b)
{
    
return a.len > b.len;
}

priority_queue 
<Edge, vector <Edge> > Q;

void InitData()
{
    
int i, u, v, len;
    memh 
= 0;
    scanf(
"%d%d"&n, &m);
    clr(E);
    
for (i = 1; i <= m; ++i)
    {
        scanf(
"%d%d%d"&u, &v, &len);
        AddEdge(u, v, len);
    }
    scanf(
"%d%d"&S, &T);
}

int Dijstra()
{
    Edge D, P;
    clr(cnt);
    clr(used);
    
for (int i = 1; i <= n; ++i)
        dist[i][
0= dist[i][1= INF;
    dist[S][
0= 0;
    cnt[S][
0= 1;
    
while (!Q.empty())
        Q.pop();
    Q.push(D.new_E(S, 
00));
    
while (!Q.empty())
    {
        P 
= Q.top();
        Q.pop();
        
if (!used[P.v][P.ref])
        {
            used[P.v][P.
ref= 1;
            
for (Edge *= E[P.v]; e; e = e -> link)
            {
                
int tmp = P.len + e -> len;
                
if (tmp < dist[e -> v][0])
                {
                    
if (dist[e -> v][0!= INF)
                    {
                        dist[e 
-> v][1= dist[e -> v][0];
                        cnt[e 
-> v][1= cnt[e -> v][0];
                        Q.push(D.new_E(e 
-> v, dist[e -> v][0], 1));
                    }
                    dist[e 
-> v][0= tmp;
                    cnt[e 
-> v][0= cnt[P.v][P.ref];
                    Q.push(D.new_E(e 
-> v, tmp, 0));
                }
                
else
                
if (tmp == dist[e -> v][0])
                {
                    cnt[e 
-> v][0+= cnt[P.v][P.ref];
                }
                
else
                
if (tmp < dist[e -> v][1])
                {
                    dist[e 
-> v][1= tmp;
                    cnt[e 
-> v][1= cnt[P.v][P.ref];
                    Q.push(D.new_E(e 
-> v, tmp, 1));
                }
                
else
                
if (dist[e -> v][1== tmp)
                {
                    cnt[e 
-> v][1+= cnt[P.v][P.ref];
                }
            }
        }
    }
    
if (dist[T][1- 1 == dist[T][0])
        cnt[T][
0+= cnt[T][1];
    
return cnt[T][0];
}

int main()
{
    
int T;
    scanf(
"%d"&T);
    
while (T--)
    {
        InitData();
        printf(
"%d\n", Dijstra());
    }
}


LLawliet 2011-10-17 16:30 鍙戣〃璇勮
]]>
POJ3522:Slim Spanhttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158548.htmlLLawlietLLawlietMon, 17 Oct 2011 07:54:00 GMThttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158548.htmlhttp://www.shnenglu.com/lvlawliet/comments/158548.htmlhttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158548.html#Feedback0http://www.shnenglu.com/lvlawliet/comments/commentRss/158548.htmlhttp://www.shnenglu.com/lvlawliet/services/trackbacks/158548.html
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



棰樼洰灝辨槸鐢熸垚涓媯墊爲錛岃姹傝竟鏉冩渶澶у噺鏈灝忕殑宸渶灝忋?br />鏍規嵁Kruskal鎬濇兂錛屾妸杈規帓搴忥紝涔嬪悗鏋氫婦涓涓嬪氨琛屼簡銆?br />
浠g爜錛?br />
#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;
}


LLawliet 2011-10-17 15:54 鍙戣〃璇勮
]]>
POJ2449錛歊emmarguts' Datehttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158540.htmlLLawlietLLawlietMon, 17 Oct 2011 07:19:00 GMThttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158540.htmlhttp://www.shnenglu.com/lvlawliet/comments/158540.htmlhttp://www.shnenglu.com/lvlawliet/archive/2011/10/17/158540.html#Feedback0http://www.shnenglu.com/lvlawliet/comments/commentRss/158540.htmlhttp://www.shnenglu.com/lvlawliet/services/trackbacks/158540.html
Remmarguts' Date
Time Limit: 4000MSMemory Limit: 65536K
Total Submissions: 12450Accepted: 3422

Description

"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a story. 

"Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One day their neighboring country sent them Princess Uyuw on a diplomatic mission." 

"Erenow, the princess sent Remmarguts a letter, informing him that she would come to the hall and hold commercial talks with UDF if and only if the prince go and meet her via the K-th shortest path. (in fact, Uyuw does not want to come at all)" 

Being interested in the trade development and such a lovely girl, Prince Remmarguts really became enamored. He needs you - the prime minister's help! 

DETAILS: UDF's capital consists of N stations. The hall is numbered S, while the station numbered T denotes prince' current place. M muddy directed sideways connect some of the stations. Remmarguts' path to welcome the princess might include the same station twice or more than twice, even it is the station with number S or T. Different paths with same length will be considered disparate. 

Input

The first line contains two integer numbers N and M (1 <= N <= 1000, 0 <= M <= 100000). Stations are numbered from 1 to N. Each of the following M lines contains three integer numbers A, B and T (1 <= A, B <= N, 1 <= T <= 100). It shows that there is a directed sideway from A-th station to B-th station with time T. 

The last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).

Output

A single line consisting of a single integer number: the length (time required) to welcome Princess Uyuw using the K-th shortest path. If K-th shortest path does not exist, you should output "-1" (without quotes) instead.

Sample Input

2 2
1 2 5
2 1 4
1 2 2

Sample Output

14

Source

POJ Monthly,Zeyuan Zhu


絎琄鐭礬闂錛屽ぇ姒傛剰鎬濆氨鏄粰浣燦涓偣錛孧鏉¤竟錛岃竟鏄湁鍚戠殑錛岀粰浣犳瘡鏉¤竟鐨勮竟鏉冿紝緇欎綘涓涓猄璧峰鐐癸紝T緇撴潫鐐癸紝鍜屼竴涓狵錛屾眰S鍒癟鐨勭K鐭礬銆?br />SPFA+A*鍚彂寮忔悳绱€?br />
璇磋鍚彂寮忔悳绱㈠惂錛?br />

閫氬父鍦ㄨВ鍐抽棶棰樼殑鏃跺欙紝鎴戜滑闇瑕佺敤鍒版悳绱㈢畻娉曪紝鐢卞凡鐭ョ姸鎬佹帹鍑烘柊鐨勭姸鎬侊紝鐒跺悗媯楠屾柊鐨勭姸鎬佹槸涓嶆槸灝辨槸鎴戜滑瑕佹眰鐨勬渶浼樿В銆傛楠屽畬鎵鏈夌殑鐘舵佸疄闄呬笂灝辯浉褰撲簬閬嶅巻浜嗕竴寮犻殣寮忓浘銆傞仐鎲劇殑鏄紝鎵鏈夌殑鐘舵佺粍鎴愮殑鐘舵佺┖闂村線寰鏄垚鎸囨暟綰у埆澧為暱鐨勶紝涔熷氨閫犳垚浜嗛亶鍘嗛渶瑕佺敤鍒版寚鏁扮駭鍒殑鏃墮棿錛屽洜姝わ紝綰補鐨勬毚鍔涙悳绱紝鏃剁┖鏁堢巼閮芥瘮杈冧綆銆傚綋鐒訛紝鎴戜滑鍦ㄧ敓媧諱腑閬囧埌浜嗙被浼間簬鎼滅儲鐨勯棶棰橈紝鎴戜滑騫朵笉浼氱洸鐩湴鍘繪悳瀵繪瘡涓縐嶇姸鎬侊紝鎴戜滑浼氶氳繃鎴戜滑鐨勬濈淮錛岄夋嫨涓鏉℃渶鎺ヨ繎浜庣洰鏍囩殑璺緞鎴栬呮槸榪戜技浜庢渶鐭殑璺緞鍘誨畬鎴愭悳绱換鍔°傚綋鎴戜滑鎯寵璁$畻鏈哄幓瀹屾垚榪欐牱涓欏規悳绱換鍔$殑鏃跺欙紝灝卞緱璁╄綆楁満鍍忎漢涓鏍瘋兘澶熷尯鍒嗗敖閲忕煭鐨勮礬寰勶紝浠ヤ究楂樻晥鍦版壘鍒版渶浼樿В銆傝繖鏃跺彲浠ユ妸璁$畻鏈虹湅浣滄槸涓縐嶆櫤鑳戒綋(agent)鍙互瀹炵幇鐢卞垵濮嬬姸鎬佸悜鐩爣鐘舵佺殑杞Щ銆?/span>

       鏈変竴縐嶈椽蹇冪瓥鐣ワ紝鍗蟲瘡涓姝ヨ漿縐婚兘鐢辮綆楁満閫夋嫨褰撳墠鐨勬渶浼樿В鐢熸垚鏂扮殑鐘舵侊紝涓鐩村埌杈劇洰鏍囩姸鎬佷負姝€傝繖鏍峰仛鐨勬椂闂存晥鐜囪櫧鐒惰緝楂橈紝浣嗘槸璐績鐨勭瓥鐣ュ彧鏄敤鍒頒簡灞閮ㄧ殑鏈浼樿В錛屽茍涓嶈兘淇濊瘉鏈鍚庡埌杈劇洰鏍囩姸鎬佸緱鍒扮殑鏄叏灞鏈浼樿В銆傚湪鑳戒繚璇佸叏灞鏈浼樿В鐨勮寖鍥村唴錛岃椽蹇冪畻娉曡繕鏄緢鏈夌敤鐨勩傛瘮濡傝鎴戜滑鐔熺煡鐨?/span>Dijkstra綆楁硶姹傚崟婧愭渶鐭礬銆傛瘡嬈¢夋嫨璺濈婧愯妭鐐規渶鐭窛紱葷殑寰呮墿灞曡妭鐐硅繘琛屾墿灞曪紝鏈鍚庡氨鑳界敓鎴愭簮鑺傜偣鍒版墍鏈夎妭鐐圭殑鏈鐭礬寰勩備笅闈細璁插埌Dijkstra鐨勬墿灞曪紝褰撶悊瑙d簡榪欎釜綆楁硶涔嬪悗錛屾垜鎯籌紝浣犱細瀵?/span>Dijkstra鏈夋洿娣卞叆鐨勭悊瑙c?/span>

       榪欏氨鏄?/span>A*綆楁硶銆傚畾涔夊垵濮嬬姸鎬?/span>S錛岀洰鏍囩姸鎬?/span>t錛?/span>g(s)鏄敱鍒濆鐘舵佽漿縐誨埌褰撳墠鐘舵?/span>s鎵緇忚繃鐨勮礬寰勯暱搴︼紝h*(s)鏄綋鍓嶇姸鎬?/span>s璺濈鐩爣鐘舵?/span>t鐨勫疄闄呴暱搴︼紝浣嗘槸涓鑸儏鍐典笅鎴戜滑鏄笉鐭ラ亾h*(s)鐨勫肩殑錛屾墍浠ヨ繕瑕佸畾涔変竴涓及浠峰嚱鏁?/span>h(s)錛屾槸瀵?/span>h*(s)鍑芥暟鍊肩殑涓嬬晫鐨勪及璁★紝涔熷氨鏄湁h(s)<=h*(s)錛岃繖鏍烽渶瑕佷竴涓潯浠訛紝浣垮緱鐢?/span>s1鐢熸垚鐨勬瘡鐘舵?/span>s2錛岄兘鏈?/span>h(s1)<=h(s2)錛岃繖鏄竴涓浉瀹圭殑浼頒環鍑芥暟銆傚啀瀹氫箟f(s)=g(s)+h(s)涓哄惎鍙戝嚱鏁幫紝鍥犱負h(s)鏄崟璋冮掑鐨勶紝鎵浠?/span>f(s)涔熸槸鍗曡皟閫掑鐨勩傝繖鏍?/span>f(s)灝變及璁″嚭浜嗙敱鍒濆鐘舵佺殑鎬諱綋浠d環銆?/span>A*綆楁硶灝遍氳繃鏋勯犺繖鏍蜂竴涓惎鍙戝嚱鏁幫紝灝嗘墍鏈夌殑寰呮墿灞曠姸鎬佸姞鍏ュ埌闃熷垪閲岋紝姣忔浠庨槦鍒楅噷閫夋嫨f(s)鍊兼渶灝忕殑鐘舵佽繘琛屾墿灞曘傜敱浜庡惎鍙戝嚱鏁扮殑浣滅敤錛屼嬌寰楄綆楁満鍦ㄨ繘琛岀姸鎬佽漿縐葷殑鏃跺欏敖閲忛伩寮浜嗕笉鍙兘浜х敓鏈浼樿В鐨勫垎鏀紝鑰岄夋嫨鐩稿杈冩帴榪戞渶浼樿В鐨勮礬寰勮繘琛屾悳绱紝鎻愰珮浜嗘悳绱㈡晥鐜囥?/span>

       璁插埌榪欓噷錛屽彲鑳藉凡緇忓A*綆楁硶鐨勬蹇墊湁鐐圭湁鐩簡銆備笅闈㈡垜鍐嶆潵鍋氫竴涓瘮杈冿紝灝辯敤涓婇潰璁插埌鐨?/span>Dijkstra鐨勪緥瀛愩?/span>Dijkstra綆楁硶璇寸殑鏄瘡嬈¢夋嫨璺濈婧愮偣鏈鐭窛紱葷殑鐐硅繘琛屾墿灞曘傚綋鐒跺彲浠ョ湅鍋氫簨鍏堝皢婧愮偣鍒版墍鏈夎妭鐐硅窛紱葷殑鍊間繚瀛樺湪涓涓紭鍏堥槦鍒楅噷錛屾瘡嬈′粠浼樺厛闃熷垪閲屽嚭闃熸渶鐭窛紱葷殑鐐規墿灞曪紝姣忎釜鐐圭殑鎵╁睍娑夊強鍒拌鏇存柊闃熷垪閲屾墍鏈夊緟鎵╁睍鑺傜偣鐨勮窛紱誨鹼紝姣忎釜鑺傜偣鍙兘榪涢槦涓嬈★紝灝遍渶瑕佹湁涓涓〃鏉ヨ褰曟瘡涓妭鐐圭殑鍏ラ槦嬈℃暟錛堝氨鏄畻娉曚腑鐢ㄥ埌鐨勬爣璁版暟緇勶級銆傚皢Dijkstra姹傛渶鐭礬鐨勬柟娉曟墿灞曪紝榪欓亾棰樼洰瑕佹眰鐨勬槸涓ょ偣闂寸k鐭礬銆傜被姣斾簬Dijkstra綆楁硶鍙互棣栧厛紜畾涓嬮潰鍑犱釜鎼滅儲絳栫暐錛?/span>

1銆?/span>鐢ㄤ紭鍏堥槦鍒椾繚瀛樿妭鐐硅繘琛屾悳绱€?/span>

2銆?/span>鏀懼紑姣忎釜鑺傜偣鐨勫叆闃熸鏁幫紝姹?/span>k鐭礬錛屾瘡涓妭鐐瑰彲浠ュ叆闃?/span>k嬈°?/span>

棣栧厛鐪嬬涓涓瓥鐣ワ紝鍦?/span>A*綆楁硶涓敤浼樺厛闃熷垪灝辨槸瑕佺敤鍒板惎鍙戝嚱鏁?/span>f(s)紜畾鐘舵佸湪浼樺厛闃熷垪閲岄潰鐨勪紭鍏堢駭銆傚叾瀹?/span>Dijkstra鐢ㄥ埌鐨勪紭鍏堥槦鍒楀疄闄呬笂灝辨槸浼頒環鍑芥暟鍊間負0錛屽惎鍙戝嚱鏁?/span>f(s)=g(s)錛屽嵆鏄夊彇鍒版簮鐐硅窛紱繪渶榪戠殑鐐硅繘琛屾墿灞曘傚洜涓?/span>h(s)=0婊¤凍浜嗕及浠峰嚱鏁扮浉瀹硅繖涓潯浠躲傝繖棰樻眰k鐭礬灝變笉鑳藉崟綰殑浣跨敤h(s)=0榪欎釜浼頒環鍑芥暟銆傝В鍐寵繖閬撻鐨勬椂鍊欓夊彇h(x)=dt(x), dt(x)鏄?/span>x鑺傜偣鍒扮洰鏍囪妭鐐圭殑鏈鐭窛紱匯傛渶鐭窛紱誨彲浠ュ紑濮嬬敱Dijkstra鐩存帴姹傚緱銆?/span>

鍐嶇湅絎簩涓瓥鐣ワ紝鎺у埗姣忎釜鑺傜偣鐨勫叆闃燂紙鎴栧嚭闃燂級嬈℃暟涓?/span>k嬈★紝鍙互鎵懼埌絎?/span>k鐭礬寰勩傚彲鑳借繖鏍鋒兂鏈夌偣涓昏鐨勫鐢紝閭d箞鎴戝氨鍏堟潵璇佹槑榪欐牱涓涓粨璁猴細

濡傛灉x鏄?/span>s鍒?/span>t鐨勭k鐭礬寰勪笂鐨勪竴涓妭鐐癸紝閭d箞鐢辮繖鏉¤礬寰?/span>s鍒?/span>x鏄?/span>s鍒?/span>x鐨勭m鐭礬寰勶紝鍒欎笉鍙兘鏈?/span>m>k銆傜敤鍙嶈瘉娉曞緢瀹規槗寰楀嚭錛氬鏋滆繖鏉¤礬寰勬槸s鍒?/span>x鐨勭m鐭礬寰勶紝濡傛灉m>k錛岄偅涔堢粡榪?/span>x鍒?/span>t鐨勮礬寰勫氨鏈?/span>m-1鏉℃瘮褰撳墠璺緞瑕佺煭錛屼笉絎﹀悎褰撳墠璺緞鏄?/span>s鍒?/span>t鐨勭k鐭礬寰勩?br />

浠g爜錛?br />
#include <cstdio>
#include 
<algorithm>
#include 
<queue>
#include 
<vector>
#include 
<cstring>
#define MAXN 10005 //杈規暟
#define inf 1 << 25
using namespace std;

int dis[MAXN];

struct node
{
    
int v, dis;
};

struct edge
{
    
int v, w;
    friend 
bool operator < (edge a, edge b)
    {
        
return (a.w + dis[a.v]) > (b.w + dis[b.v]);
    }
};

vector 
<node> map[MAXN];
vector 
<node> remap[MAXN];

int n, m; //n鏄妭鐐規暟錛宮鏄竟鏁般?/span>
int s, t, k;  //s鏄搗濮嬬偣錛宼鏄粨鏉熺偣錛宬鏄k灝忋?/span>

void init()
{
    
int i;
    
for (i = 0; i < MAXN; ++i)
        map[i].clear();
    
for (i = 0; i < MAXN; ++i)
        remap[i].clear();
}

void spfa(int s)
{
    
int i;
    
bool used[MAXN];
    memset(used, 
0sizeof(used));
    
for (i = 0; i < MAXN; ++i)
        dis[i] 
= inf;
    dis[s] 
= 0;
    used[s] 
= true;
    queue 
<int> q;
    q.push(s);
    
while (!q.empty())
    {
        
int u = q.front();
        q.pop();
        used[u] 
= false;
        
for (i = 0; i < remap[u].size(); ++i)
        {
            node p 
= remap[u][i];
            
if (dis[p.v] > dis[u] + p.dis)
            {
                dis[p.v] 
= dis[u] + p.dis;
                
if (!used[p.v])
                {
                    used[p.v] 
= true;
                    q.push(p.v);
                }
            }
        }
    }
}

int a_star()
{
    
if (s == t)
        k
++;   //娉ㄦ剰錛岃搗濮嬪拰緇撴潫涓鏍鳳紝k瑕?1錛?/span>
    if (dis[s] == inf)
        
return -1;
    
int i, x, len, cnt[MAXN];
    edge n1, n2;
    priority_queue 
<edge> q;
    memset(cnt, 
0sizeof(cnt));
    n1.v 
= s;
    n1.w 
= 0;
    q.push(n1);
    
while (!q.empty())
    {
        n2 
= q.top();
        q.pop();
        x 
= n2.v;
        len 
= n2.w;
        cnt[x]
++;
        
if (cnt[t] == k)
            
return len;
        
if (cnt[x] > k)
            
continue;
        
for (i = 0; i < map[n2.v].size(); ++i)
        {
            n1.v 
= map[n2.v][i].v;
            n1.w 
= len + map[n2.v][i].dis;
            q.push(n1);
        }
    }
    
return -1;
}

int main()
{
    
int i;
    node p;
    
while (scanf("%d%d"&n, &m) != EOF)
    {
        init();
        
int a, b, l;
        
for (i = 1; i <= m; ++i)
        {
            scanf(
"%d%d%d"&a, &b, &l);
            p.v 
= b;
            p.dis 
= l;
            map[a].push_back(p);
            p.v 
= a;
            remap[b].push_back(p);
        }
        scanf(
"%d%d%d"&s, &t, &k);
        spfa(t);
        
int ans = a_star();
        printf(
"%d\n", ans);
    }
    
return 0;
}


LLawliet 2011-10-17 15:19 鍙戣〃璇勮
]]>
91精品国产高清久久久久久91| 三级三级久久三级久久| 午夜精品久久久久久毛片| 潮喷大喷水系列无码久久精品| 久久97久久97精品免视看| 久久亚洲春色中文字幕久久久 | 97久久综合精品久久久综合| 久久综合九色欧美综合狠狠| 久久精品无码一区二区三区| 日韩人妻无码精品久久免费一| 国产产无码乱码精品久久鸭 | 久久国产精品国语对白| 久久天天婷婷五月俺也去| 精品水蜜桃久久久久久久| 精品久久久久成人码免费动漫 | 色综合久久中文色婷婷| 久久人妻少妇嫩草AV无码专区| 久久久精品人妻一区二区三区蜜桃 | 伊人久久精品影院| 香蕉aa三级久久毛片| 99久久精品免费观看国产| 久久精品国产亚洲αv忘忧草| 免费精品99久久国产综合精品| 一级A毛片免费观看久久精品| 久久99热狠狠色精品一区| 精品久久久一二三区| 精品久久久久久国产三级| 久久精品成人国产午夜| 久久久久无码精品国产| 亚洲伊人久久综合中文成人网| 久久这里只有精品久久| 2021精品国产综合久久| 亚洲AV日韩精品久久久久久| 欧美久久一级内射wwwwww.| 国产精品亚洲美女久久久| 亚洲精品国产自在久久| 久久se精品一区二区影院 | 精品一二三区久久aaa片| 狠狠色丁香久久婷婷综合_中| 久久影院亚洲一区| 欧美精品乱码99久久蜜桃|