锘??xml version="1.0" encoding="utf-8" standalone="yes"?>99久久精品国产一区二区蜜芽
,久久精品成人免费网站,国产一区二区久久久http://www.shnenglu.com/lzmagic/category/10078.html鍗氬 瀹¢棶 鎱庢?鏄庤鯨 絎冭zh-cnSat, 11 Apr 2009 11:28:04 GMTSat, 11 Apr 2009 11:28:04 GMT60- Floyd_Warshall綆楁硶http://www.shnenglu.com/lzmagic/archive/2009/04/11/79566.htmllzmagiclzmagicFri, 10 Apr 2009 19:14:00 GMThttp://www.shnenglu.com/lzmagic/archive/2009/04/11/79566.htmlhttp://www.shnenglu.com/lzmagic/comments/79566.htmlhttp://www.shnenglu.com/lzmagic/archive/2009/04/11/79566.html#Feedback0http://www.shnenglu.com/lzmagic/comments/commentRss/79566.htmlhttp://www.shnenglu.com/lzmagic/services/trackbacks/79566.html

/**//**
* FLOYD_WARSHALL 鎵鏈夐《鐐瑰鐨勬渶鐭礬寰勭畻娉?nbsp;(All-Pairs Shortest Path Algorithm)
* 杈撳叆錛氬浘g
* 杈撳嚭錛氭墍鏈夐《鐐瑰鐨勬渶鐭礬寰勯暱
* 緇撴瀯錛氬浘g鐢ㄩ偦鎺ョ煩闃佃〃紺?br>
* 綆楁硶錛欶loyd_Warshall綆楁硶(鍔ㄦ佽鍒?
* 澶嶆潅搴︼細O(|V|^3)
*/
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <stack>
#include <queue>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <functional>
#include <climits>
using namespace std;

int n;
vector<vector<int> > g;
vector<vector<int> > dist;

void Floyd_Warshall()


{
// 鍒濆鍖杁ist錛岄《鐐歸棿(鏃犱腑闂撮《鐐?鏈鐭礬寰勯暱涓鴻竟闀匡紝欏剁偣鍒拌嚜韜殑鏈鐭礬寰勯暱涓?銆?nbsp;
dist = g;
for (int i = 0; i < n; ++i)
dist[i][i] = 0;
// 浠庨《鐐筰鍒板畾鐐筳涓斾腑闂撮《鐐圭殕灞炰簬闆嗗悎{0, 1, 2,
, k}鐨勬渶鐭礬寰勯暱銆?nbsp;
for (int k = 0; k < n; ++k)
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (dist[i][k] < INT_MAX && dist[k][j] < INT_MAX)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}

int main()


{
n = 5;
g.assign(n, vector<int>(n, INT_MAX));
g[0][1] = 3; g[0][2] = 8; g[0][4] = -4;
g[1][3] = 1; g[1][4] = 7;
g[2][1] = 4;
g[3][0] = 2; g[3][2] = -5;
g[4][3] = 6;
Floyd_Warshall();
for (int i = 0; i < n; ++i)

{
for (int j = 0; j < n; ++j)
cout << dist[i][j] << ' ';
cout << endl;
}
system("pause");
return 0;
}

]]> - Kruskal綆楁硶http://www.shnenglu.com/lzmagic/archive/2009/04/10/79542.htmllzmagiclzmagicFri, 10 Apr 2009 11:41:00 GMThttp://www.shnenglu.com/lzmagic/archive/2009/04/10/79542.htmlhttp://www.shnenglu.com/lzmagic/comments/79542.htmlhttp://www.shnenglu.com/lzmagic/archive/2009/04/10/79542.html#Feedback0http://www.shnenglu.com/lzmagic/comments/commentRss/79542.htmlhttp://www.shnenglu.com/lzmagic/services/trackbacks/79542.html

/**//**
* KRUSKAL 鏈灝忕敓鎴愭爲綆楁硶 (Minimum Spanning Tree)
* 杈撳叆錛氬浘g錛?nbsp; // 鏈夊悜鍥炬垨鑰呮棤鍚戝浘
* 杈撳嚭錛?1)鏈灝忕敓鎴愭爲闀縮um錛?nbsp;
* (2)鏈灝忕敓鎴愭爲mst銆?br>
* 緇撴瀯: 鍥緂鐢ㄩ《鐐規暟n鍜岃竟闆唀dges(浼樺厛闃熷垪)琛ㄧず錛屼袱鐐規槸鍚﹁繛閫氱敤騫舵煡闆嗗疄鐜般?nbsp;
* 綆楁硶錛欿ruskal綆楁硶
* 澶嶆潅搴︼細O(|E|*log|E|)~O(|E|*log|V|)
*/
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <functional>
#include <climits>
using namespace std;

struct Edge


{
int u, v, w; // u銆乿:涓や釜欏剁偣 w:鏉?nbsp;

Edge()
{ }

Edge(int u0, int v0, int w0):u(u0), v(v0), w(w0)
{ }
};

int n; // n : 欏剁偣涓暟
vector<Edge> edges; // edges : 鍥緂鐨勬墍鏈夎竟
int sum; // sum : 鏈灝忕敓鎴愭爲闀?nbsp;
vector<Edge> mst; // mst : 鏈灝忕敓鎴愭爲(鐢ㄨ竟闆嗚〃紺?

class DisjSets


{
vector<int> s;
public:

DisjSets(int n) : s(n, -1)
{ };
int find (int x)

{
if (s[x] < 0) return x;
else return s[x] = find(s[x]); // 鍘嬬緝璺緞銆?nbsp;
};
void unionSets(int root1, int root2)

{
if (s[root1] > s[root2]) // 鎸変釜鏁版眰騫?涓暟鐢ㄨ礋鏁拌〃紺?銆?nbsp;

{
s[root2] += s[root1];
s[root1] = root2;
}
else

{
s[root1] += s[root2];
s[root2] = root1;
}
};
};

bool Cmp(const Edge &lhs, const Edge &rhs)


{
return lhs.w > rhs.w;
}

bool Kruskal()


{
DisjSets ds(n);
make_heap(edges.begin(), edges.end(), Cmp); // 瀵硅竟闆嗗緩鍫嗐?nbsp;
int root1, root2;
Edge e;
while (!edges.empty()) // 閬嶅巻鎵鏈夎竟錛?nbsp;

{
e = edges.front(); pop_heap(edges.begin(), edges.end(), Cmp); edges.pop_back();// 浠庢湭閫夎竟闆嗕腑瀵繪壘鏈灝忔潈鐨勮竟e銆?nbsp;
root1 = ds.find(e.u), root2 = ds.find(e.v); // 鑾峰彇u銆乿鎵鍦ㄧ殑鐐歸泦錛?nbsp;
if (root1 != root2) // 濡傛灉u銆乿涓嶆槸鍚屼竴涓偣闆嗭紝

{
sum += e.w; // 璋冩暣鏈灝忕敓鎴愭爲闀?nbsp;
mst.push_back(e); // 鎶婅竟e鏀懼叆鏈灝忕敓鎴愭爲mst涓紝
ds.unionSets(root1, root2); // 鍚堝茍涓ょ偣鎵鍦ㄧ殑鐐歸泦銆?nbsp;
}
if (mst.size() == n - 1) return true; // 濡傛灉閫夊彇杈逛釜鏁頒負n - 1錛屾垚鍔熴?/span>
}
return false;
}

int main()


{
n = 7;
edges.clear();
edges.push_back(Edge(0, 1, 2)); edges.push_back(Edge(0, 2, 4)); edges.push_back(Edge(0, 3, 1));
edges.push_back(Edge(1, 3, 3)); edges.push_back(Edge(1, 4, 10));
edges.push_back(Edge(2, 3, 2)); edges.push_back(Edge(2, 5, 5));
edges.push_back(Edge(3, 4, 7)); edges.push_back(Edge(3, 5, 8)); edges.push_back(Edge(3, 6, 4));
edges.push_back(Edge(4, 6, 6));
edges.push_back(Edge(5, 6, 1));
sum = 0;
mst.clear();
if(Kruskal())

{
cout << sum << endl;
for (vector<Edge>::iterator it = mst.begin(); it != mst.end(); ++it)
cout << it->u << "->" << it->v << endl;
}
else

{
cout << "Some vertex cann't be reached." << endl;
}
system("pause");
return 0;
}

]]> - Prim綆楁硶http://www.shnenglu.com/lzmagic/archive/2009/04/10/79536.htmllzmagiclzmagicFri, 10 Apr 2009 11:11:00 GMThttp://www.shnenglu.com/lzmagic/archive/2009/04/10/79536.htmlhttp://www.shnenglu.com/lzmagic/comments/79536.htmlhttp://www.shnenglu.com/lzmagic/archive/2009/04/10/79536.html#Feedback0http://www.shnenglu.com/lzmagic/comments/commentRss/79536.htmlhttp://www.shnenglu.com/lzmagic/services/trackbacks/79536.html

/**//**
* PRIM(綆鍗曠増) 鏈灝忕敓鎴愭爲綆楁硶 (Minimum Spanning Tree)
* 杈撳叆錛氬浘g錛?nbsp; // 鏈夊悜鍥炬垨鑰呮棤鍚戝浘
* 杈撳嚭錛?1)鏈灝忕敓鎴愭爲闀縮um錛?nbsp;
* (2)鏈灝忕敓鎴愭爲prev銆?br>
* 緇撴瀯: 鍥緂鐢ㄩ偦鎺ョ煩闃佃〃紺猴紝鏈鐭竟闀縟ist鐢ㄦ暟緇勮〃紺恒?nbsp;
* 綆楁硶錛歅rim綆楁硶
* 澶嶆潅搴︼細O(|V|^2)
*/
#include <iostream>
#include <vector>
#include <list>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <functional>
#include <climits>
using namespace std;

int n; // n : 欏剁偣涓暟
vector<vector<int> > g; // g : 鍥?graph)(鐢ㄩ偦鎺ョ煩闃?adjacent matrix)琛ㄧず)
vector<bool> known; // known : 鍚勭偣鏄惁宸茬粡閫夊彇
vector<int> dist; // dist : 宸查夊彇鐐歸泦鍒版湭閫夊彇鐐圭殑鏈灝忚竟闀?nbsp;
vector<int> prev; // prev : 鏈灝忕敓鎴愭爲涓悇鐐圭殑鍓嶄竴欏剁偣
int s; // s : 璧風偣(start)
int sum; // sum : 鏈灝忕敓鎴愭爲闀?nbsp;

bool Prim() // 璐績綆楁硶(Greedy Algorithm)


{
known.assign(n, false);
dist.assign(n, INT_MAX);
prev.resize(n); // 鍒濆鍖杒nown銆乨ist銆乸rev銆?nbsp;
dist[s] = 0; // 鍒濆鍖栬搗鐐瑰埌鑷韓鐨勮礬寰勯暱涓?銆?/span>
int i;
for (i = 0; i < n; ++i)

{
int min = INT_MAX, v;
for (int i = 0; i < n; ++i)
if (!known[i] && min > dist[i])
min = dist[i], v = i; // 瀵繪壘鏈煡鐨勬渶鐭礬寰勯暱鐨勯《鐐箆錛?nbsp;
if (min == INT_MAX) break; // 濡傛灉鎵句笉鍒幫紝閫鍑猴紱
known[v] = true; // 濡傛灉鎵懼埌錛屽皢欏剁偣v璁句負宸茬煡錛?/span>
sum += dist[v]; // 璋冩暣鏈灝忕敓鎴愭爲闀?nbsp;
for (int w = 0; w < n; ++w) // 閬嶅巻鎵鏈塿鎸囧悜鐨勯《鐐箇錛?nbsp;
if (!known[w] && g[v][w] < INT_MAX && dist[w] > g[v][w])
dist[w] = g[v][w], prev[w] = v; // 璋冩暣欏剁偣w鐨勬渶鐭礬寰勯暱dist鍜屾渶鐭礬寰勭殑鍓嶄竴欏剁偣 prev銆?nbsp;
}
return i == n; // 濡傛灉閫夊彇欏剁偣涓暟涓簄錛屾垚鍔熴?nbsp;
}

int main()


{
n = 7;
g.assign(n, vector<int>(n, INT_MAX));
g[0][1] = g[1][0] = 2; g[0][2] = g[2][0] = 4; g[0][3] = g[3][0] = 1;
g[1][3] = g[3][1] = 3; g[1][4] = g[4][1] = 10;
g[2][3] = g[3][2] = 2; g[2][5] = g[5][2] = 5;
g[3][4] = g[4][3] = 7; g[3][5] = g[5][3] = 8; g[3][6] = g[6][3] = 4;
g[4][6] = g[6][4] = 6;
g[5][6] = g[6][5] = 1;
s = 0; // 璧風偣浠婚?nbsp;
sum = 0;
if (Prim())

{
cout << sum << endl;
for (int i = 1; i < n; ++i)
if(i != s) cout << prev[i] << "->" << i << endl;
}
else

{
cout << "Some vertex cann't be reached." << endl;
}
system("pause");
return 0;
}

]]> - Critical Path 鍏抽敭璺緞 http://www.shnenglu.com/lzmagic/archive/2009/04/09/79394.htmllzmagiclzmagicThu, 09 Apr 2009 15:02:00 GMThttp://www.shnenglu.com/lzmagic/archive/2009/04/09/79394.htmlhttp://www.shnenglu.com/lzmagic/comments/79394.htmlhttp://www.shnenglu.com/lzmagic/archive/2009/04/09/79394.html#Feedback0http://www.shnenglu.com/lzmagic/comments/commentRss/79394.htmlhttp://www.shnenglu.com/lzmagic/services/trackbacks/79394.html闃呰鍏ㄦ枃

]]> - Bellman_Ford綆楁硶 SPFA綆楁硶http://www.shnenglu.com/lzmagic/archive/2009/04/09/79359.htmllzmagiclzmagicThu, 09 Apr 2009 11:16:00 GMThttp://www.shnenglu.com/lzmagic/archive/2009/04/09/79359.htmlhttp://www.shnenglu.com/lzmagic/comments/79359.htmlhttp://www.shnenglu.com/lzmagic/archive/2009/04/09/79359.html#Feedback0http://www.shnenglu.com/lzmagic/comments/commentRss/79359.htmlhttp://www.shnenglu.com/lzmagic/services/trackbacks/79359.html闃呰鍏ㄦ枃

]]> - Dijkstra綆楁硶http://www.shnenglu.com/lzmagic/archive/2009/04/09/79329.htmllzmagiclzmagicThu, 09 Apr 2009 02:45:00 GMThttp://www.shnenglu.com/lzmagic/archive/2009/04/09/79329.htmlhttp://www.shnenglu.com/lzmagic/comments/79329.htmlhttp://www.shnenglu.com/lzmagic/archive/2009/04/09/79329.html#Feedback0http://www.shnenglu.com/lzmagic/comments/commentRss/79329.htmlhttp://www.shnenglu.com/lzmagic/services/trackbacks/79329.html

/**//**
* DIJKSTRA(綆鍗曠増) 鍗曟簮鏈鐭礬寰勭畻娉曪紙涓嶅厑璁稿瓨鍦ㄨ礋杈癸級
* 杈撳叆錛?1)鍥緂錛?nbsp; // 鏈夊悜鍥炬垨鑰呮棤鍚戝浘
* (2)婧愮偣s銆?nbsp;
* 杈撳嚭錛?1)婧愮偣s鍒板悇鐐圭殑鏈鐭礬寰勯暱dist錛?nbsp;
* (2)婧愮偣s鍒板悇鐐圭殑鏈鐭礬寰刾rev銆?br>
* 緇撴瀯: 鍥緂鐢ㄩ偦鎺ョ煩闃佃〃紺猴紝鏈鐭礬寰勯暱dist鐢ㄦ暟緇勮〃紺恒?nbsp;
* 綆楁硶錛欴ijkstra綆楁硶
* 澶嶆潅搴︼細O(|V|^2)
*/
#include <iostream>
#include <vector>
#include <list>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <functional>
#include <climits>
using namespace std;

int n; // n : 欏剁偣涓暟
vector<vector<int> > g; // g : 鍥?graph)(鐢ㄩ偦鎺ョ煩闃?adjacent matrix)琛ㄧず)
int s; // s : 婧愮偣(source)
vector<bool> known; // known : 鍚勭偣鏄惁鐭ラ亾鏈鐭礬寰?nbsp;
vector<int> dist; // dist : 婧愮偣s鍒板悇鐐圭殑鏈鐭礬寰勯暱
vector<int> prev; // prev : 鍚勭偣鏈鐭礬寰勭殑鍓嶄竴欏剁偣

void Dijkstra() // 璐績綆楁硶(Greedy Algorithm)


{
known.assign(n, false);
dist.assign(n, INT_MAX);
prev.resize(n); // 鍒濆鍖杒nown銆乨ist銆乸rev銆?nbsp;
dist[s] = 0; // 鍒濆鍖栨簮鐐箂鍒拌嚜韜殑璺緞闀夸負0銆?nbsp;
for (;;)

{
int min = INT_MAX, v = s;
for (int i = 0; i < n; ++i)
if (!known[i] && min > dist[i])
min = dist[i], v = i; // 瀵繪壘鏈煡鐨勬渶鐭礬寰勯暱鐨勯《鐐箆錛?nbsp;
if (min == INT_MAX) break; // 濡傛灉鎵句笉鍒幫紝閫鍑猴紱
known[v] = true; // 濡傛灉鎵懼埌錛屽皢欏剁偣v璁句負宸茬煡錛?nbsp;
for (int w = 0; w < n; ++w) // 閬嶅巻鎵鏈塿鎸囧悜鐨勯《鐐箇錛?nbsp;
if (!known[w] && g[v][w] < INT_MAX && dist[w] > dist[v] + g[v][w])
dist[w] = dist[v] + g[v][w], prev[w] = v; // 璋冩暣欏剁偣w鐨勬渶鐭礬寰勯暱dist鍜屾渶鐭礬寰勭殑鍓嶄竴欏剁偣 prev銆?nbsp;
}
}

void Print_SP(int v)


{
if (v != s) Print_SP(prev[v]);
cout << v << " ";
}

int main()


{
n = 7;
g.assign(n, vector<int>(n, INT_MAX));
g[0][1] = 2; g[0][3] = 1;
g[1][3] = 3; g[1][4] = 10;
g[2][0] = 4; g[2][5] = 5;
g[3][2] = 2; g[3][4] = 2; g[3][5] = 8; g[3][6] = 4;
g[4][6] = 6;
g[6][5] = 1;
s = 0;
Dijkstra();
copy(dist.begin(), dist.end(), ostream_iterator<int>(cout, " ")); cout << endl;
for (int i = 0; i < n; ++i)
if(dist[i] != INT_MAX)

{
cout << s << "->" << i << ": ";
Print_SP(i);
cout << endl;
}
system("pause");
return 0;
}

]]> - USP 鏃犳潈鏈鐭礬寰勭畻娉?/title>http://www.shnenglu.com/lzmagic/archive/2009/04/09/79328.htmllzmagiclzmagicThu, 09 Apr 2009 02:44:00 GMThttp://www.shnenglu.com/lzmagic/archive/2009/04/09/79328.htmlhttp://www.shnenglu.com/lzmagic/comments/79328.htmlhttp://www.shnenglu.com/lzmagic/archive/2009/04/09/79328.html#Feedback0http://www.shnenglu.com/lzmagic/comments/commentRss/79328.htmlhttp://www.shnenglu.com/lzmagic/services/trackbacks/79328.html

/**//**
* USP 鏃犳潈鏈鐭礬寰勭畻娉?Unweighted Shortest Path Algorithm)
* 杈撳叆錛?1)鍥緂錛?nbsp; // 鏈夊悜鍥炬垨鑰呮棤鍚戝浘
* (2)婧愮偣s銆?nbsp;
* 杈撳嚭錛?1)婧愮偣s鍒板悇鐐圭殑鏃犳潈鏈鐭礬寰勯暱dist(璺緞鐨勮竟鏁版渶灝?錛?nbsp;
* (2)婧愮偣s鍒板悇鐐圭殑鏃犳潈鏈鐭礬寰刾rev銆?nbsp;
* 緇撴瀯: 鍥緂鐢ㄩ偦鎺ヨ〃琛ㄧず錛屾渶鐭礬寰勯暱dist鐢ㄦ暟緇勮〃紺恒?nbsp;
* 綆楁硶錛氬箍搴︿紭鍏堟悳绱?BFS)
* 澶嶆潅搴︼細O(|E|+|V|)~O(|E|)
*/
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <functional>
#include <climits>
using namespace std;

int n; // n : 欏剁偣涓暟
vector<list<int> > g; // g : 鍥?graph)(鐢ㄩ偦鎺ヨ〃(adjacent list)琛ㄧず)
int s; // s : 婧愮偣(source)
vector<int> dist; // dist : 婧愮偣s鍒板悇鐐逛箣闂寸殑璺濈
vector<int> prev; // prev : 鍚勭偣鏈鐭礬寰勭殑鍓嶄竴欏剁偣鍙?nbsp;

void USP() // 騫垮害浼樺厛鎼滅儲(BFS)


{
queue<int> que;
dist.assign(n, INT_MAX); // 鍒濆鍖杁ist錛?nbsp;
prev.resize(n); // 鍒濆鍖杙rev銆?nbsp;
dist[s] = 0; que.push(s); // s鍒拌嚜韜窛紱諱負0錛宻鍏ラ槦銆?nbsp;
while (!que.empty())

{
int v = que.front(); que.pop(); // v鍑洪槦錛?nbsp;
for (list<int>::iterator it = g[v].begin(); it != g[v].end(); ++it) // 閬嶅巻v鐩擱偦鐐?it錛?nbsp;
if (dist[*it] == INT_MAX) // 濡傛灉*it鏈闂紝

{
dist[*it] = dist[v] + 1; prev[*it] = v; // 璋冩暣鐐?it錛?nbsp;
que.push(*it); // *it鍏ラ槦銆?nbsp;
}
}
}

void Print_SP(int v)


{
if (v != s) Print_SP(prev[v]);
cout << v << " ";
}

int main()


{
n = 7;
g.assign(n, list<int>());
g[0].push_back(1); g[0].push_back(3);
g[1].push_back(3); g[1].push_back(4);
g[2].push_back(0); g[2].push_back(5);
g[3].push_back(2); g[3].push_back(4); g[3].push_back(5); g[3].push_back(6);
g[4].push_back(6);
g[6].push_back(5);
s = 2;
USP();
copy(dist.begin(), dist.end(), ostream_iterator<int>(cout, " ")); cout << endl;
for (int i = 0; i < n; ++i)
if(dist[i] != INT_MAX)

{
cout << s << "->" << i << ": ";
Print_SP(i);
cout << endl;
}
system("pause");
return 0;
}

]]> - Topsort 鎷撴墤鎺掑簭http://www.shnenglu.com/lzmagic/archive/2009/04/06/79066.htmllzmagiclzmagicMon, 06 Apr 2009 01:40:00 GMThttp://www.shnenglu.com/lzmagic/archive/2009/04/06/79066.htmlhttp://www.shnenglu.com/lzmagic/comments/79066.htmlhttp://www.shnenglu.com/lzmagic/archive/2009/04/06/79066.html#Feedback2http://www.shnenglu.com/lzmagic/comments/commentRss/79066.htmlhttp://www.shnenglu.com/lzmagic/services/trackbacks/79066.html

/**//**
* TOPSORT(綆鍗曠増) 鎷撴墤鎺掑簭(Topological Sort)
* 杈撳叆錛氭湁鍚戝浘g
* 杈撳嚭錛氭槸鍚﹀瓨鍦ㄦ嫇鎵戞帓搴忥紝濡傛灉瀛樺湪錛岃幏鍙栨嫇鎵戞帓搴忓簭鍒梥eq
* 緇撴瀯錛氬浘g鐢ㄩ偦鎺ョ煩闃佃〃紺?br>
* 綆楁硶錛氬箍搴︿紭鍏堟悳绱?BFS)
* 澶嶆潅搴︼細O(|V|^2)
*/
#include <iostream>
#include <vector>
#include <queue>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <climits>
using namespace std;

int n; // n 錛氶《鐐逛釜鏁?nbsp;
vector<vector<int> > g; // g 錛氬浘(graph)(鐢ㄩ偦鎺ョ煩闃?adjacent matrix)琛ㄧず)
vector<int> seq; // seq 錛氭嫇鎵戝簭鍒?sequence)

bool TopSort()


{
vector<int> inc(n, 0);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (g[i][j] < INT_MAX) ++inc[j]; // 璁$畻姣忎釜欏剁偣鐨勫叆搴︼紝
queue<int> que;
for (int j = 0; j < n; ++j)
if (inc[j] == 0) que.push(j); // 濡傛灉欏剁偣鐨勫叆搴︿負0錛屽叆闃熴?/span>
int seqc = 0;
seq.resize(n);
while (!que.empty()) // 濡傛灉闃熷垪que闈炵┖錛?/span>

{
int v = que.front(); que.pop();
seq[seqc++] = v; // 欏剁偣v鍑洪槦錛屾斁鍏eq涓紝
for (int w = 0; w < n; ++w) // 閬嶅巻鎵鏈塿鎸囧悜鐨勯《鐐箇錛?/span>
if (g[v][w] < INT_MAX)
if (--inc[w] == 0) que.push(w); // 璋冩暣w鐨勫叆搴︼紝濡傛灉w鐨勫叆搴︿負0錛屽叆闃熴?nbsp;
}
return seqc == n; // 濡傛灉seq宸插鐞嗛《鐐規暟涓簄錛屽瓨鍦ㄦ嫇鎵戞帓搴忥紝鍚﹀垯瀛樺湪鍥炶礬銆?/span>
}

int main()


{
n = 7;
g.assign(n, vector<int>(n, INT_MAX));
g[0][1] = 1, g[0][2] = 1, g[0][3] = 1;
g[1][3] = 1, g[1][4] = 1;
g[2][5] = 1;
g[3][2] = 1, g[3][5] = 1, g[3][6] = 1;
g[4][3] = 1, g[4][6] = 1;
g[6][5] = 1;

if (TopSort())

{
copy(seq.begin(), seq.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
else

{
cout << "circles exist" << endl;
}
system("pause");
return 0;
}

]]> - (姝e垯琛ㄨ揪寮?鏄惁鍖歸厤(瀛楃涓?http://www.shnenglu.com/lzmagic/archive/2009/04/04/78977.htmllzmagiclzmagicSat, 04 Apr 2009 14:58:00 GMThttp://www.shnenglu.com/lzmagic/archive/2009/04/04/78977.htmlhttp://www.shnenglu.com/lzmagic/comments/78977.htmlhttp://www.shnenglu.com/lzmagic/archive/2009/04/04/78977.html#Feedback2http://www.shnenglu.com/lzmagic/comments/commentRss/78977.htmlhttp://www.shnenglu.com/lzmagic/services/trackbacks/78977.html

/**//**
* IS_MATCHED
* 杈撳叆錛?1)涓涓鍒欒〃杈懼紡錛氬甫鏈夋鍒欑鍙?*'銆?+'銆??'銆?.'(姝e垯絎﹀彿涓嶇浉閭?錛?nbsp;
* '*'錛氫換鎰忓涓換鎰忓瓧絎︼紱
* '+'錛氳嚦灝?涓換鎰忓瓧絎︼紱
* '?'錛?涓垨1涓換鎰忓瓧絎︼紱
* '.'錛氭伆濂?涓換鎰忓瓧絎︺?nbsp;
* (2)涓涓瓧絎︿覆銆?nbsp;
* 杈撳嚭錛氳姝e垯琛ㄨ揪寮忔槸鍚﹀尮閰嶈瀛楃銆?nbsp;
*/
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

bool Is_Matched(string ®, string &str)


{
bool is_asterisk, is_plus, is_question, is_dot, is_ok(true);
string substr;
string::iterator reg_it(reg.begin()), str_it(str.begin()), str_it2;
while (is_ok && reg_it != reg.end())

{
// 鍒嗙姝e垯絎﹀彿緇檌s_asterisk銆乮s_plus銆乮s_question銆乮s_dot銆?nbsp;
is_asterisk = *reg_it == '*';
is_plus = *reg_it == '+';
is_question = *reg_it == '?';
is_dot = *reg_it == '.';
if ( is_asterisk || is_plus || is_question || is_dot) ++reg_it;
// 鍒嗙瀛愬瓧絎︿覆緇檚ubstr銆?nbsp;
substr.clear();
for (reg_it = reg_it; reg_it != reg.end()
&& *reg_it != '*'
&& *reg_it != '+'
&& *reg_it != '?'
&& *reg_it != '.'; ++reg_it)// 璋冩暣reg_it銆?nbsp;
substr.push_back(*reg_it);
// 鍖歸厤瀛愬瓧絎︿覆錛歴ubstr絀猴紝str_it2=>end()錛屽惁鍒檚tr_it2=>鍖歸厤鐐規垨end()銆?nbsp;
if (substr.empty()) str_it2 = str.end();
else str_it2 = search(str_it, str.end(), substr.begin(), substr.end());
// 鏄惁鍖歸厤姝e垯絎﹀彿錛歩s_asterisk涓嶅仛鍒ゆ柇銆?nbsp;
if (is_plus && str_it == str_it2
|| is_question && str_it != str_it2 && str_it != str_it2 - 1
|| is_dot && str_it != str_it2 - 1) is_ok = false;
// 鏄惁鍖歸厤瀛愬瓧絎︿覆銆?nbsp;
if (!substr.empty() && str_it2 == str.end()) is_ok = false;
else str_it = str_it2 + substr.size(); // 璋冩暣str_it銆?nbsp;
}
// 鍖歸厤鎴愬姛錛歩s_ok涓簍rue錛宺eg_it鍒拌揪reg.end()錛宻tr_it鍒拌揪str.end()銆?nbsp;
return is_ok && reg_it == reg.end() && str_it == str.end();
}

int main ()


{
string reg, str; // input : *l?m.g+ lzmagic ?zm lzmagic
while (cin >> reg >> str)

{
if (Is_Matched(reg, str)) cout << "match" << endl;
else cout << "dismatch" << endl;
}
system("pause");
return 0;
}

]]> - Quicksort 蹇熸帓搴?/title>http://www.shnenglu.com/lzmagic/archive/2009/04/03/78774.htmllzmagiclzmagicFri, 03 Apr 2009 01:53:00 GMThttp://www.shnenglu.com/lzmagic/archive/2009/04/03/78774.htmlhttp://www.shnenglu.com/lzmagic/comments/78774.htmlhttp://www.shnenglu.com/lzmagic/archive/2009/04/03/78774.html#Feedback2http://www.shnenglu.com/lzmagic/comments/commentRss/78774.htmlhttp://www.shnenglu.com/lzmagic/services/trackbacks/78774.html

/**//**
* QUICKSORT : sort
* void sort(iterator beg, iterator end);
* void sort(iterator beg, iterator end, compare cmp); <algorithm>
*/
#include <iostream>
#include <iterator>
#include <algorithm>
#include <numeric>
using namespace std;

int Partition(int *a, int lhs, int rhs)


{
int key = a[rhs];
int i = lhs - 1;
for (int j = lhs; j < rhs; ++j)
if (a[j] <= key) swap(a[++i], a[j]);
swap(a[++i], a[rhs]);
return i;
}

void QuickSort(int *a, int lhs, int rhs)


{
if (lhs < rhs)

{
int mid = Partition(a, lhs, rhs);
QuickSort(a, lhs, mid - 1);
QuickSort(a, mid + 1, rhs);
}
}

int main()


{
int a[100];
for (int i = 0; i < 100; ++i) a[i] = i;
random_shuffle(a, a + 100);
copy(a, a + 100, ostream_iterator<int>(cout, " ")); cout << endl << endl;
QuickSort(a, 0, 99);
copy(a, a + 100, ostream_iterator<int>(cout, " ")); cout << endl;
system("pause");
return 0;
}

]]>
国内精品久久久久久久涩爱|
99精品久久久久久久婷婷|
久久99中文字幕久久|
国产精品99久久久久久www|
久久精品国产亚洲av瑜伽|
久久精品综合网|
久久精品国产久精国产思思|
国产高潮国产高潮久久久91|
久久久久久精品成人免费图片|
精品无码久久久久久午夜|
亚洲а∨天堂久久精品|
久久国产精品成人片免费|
久久人人爽人人澡人人高潮AV|
婷婷久久香蕉五月综合加勒比|
品成人欧美大片久久国产欧美|
久久99国产精品尤物|
色综合久久夜色精品国产|
99久久精品免费看国产|
久久国产色av免费看|
久久久噜噜噜久久中文字幕色伊伊|
欧美熟妇另类久久久久久不卡|
久久精品国产亚洲精品|
中文字幕亚洲综合久久2|
奇米综合四色77777久久|
久久精品亚洲AV久久久无码|
精品久久久久久国产牛牛app|
久久精品国产亚洲av麻豆色欲|
18岁日韩内射颜射午夜久久成人|
久久噜噜久久久精品66|
久久久国产精品网站|
国产午夜福利精品久久2021|
亚洲国产精品久久久天堂
|
久久久久久久波多野结衣高潮|
久久精品国产色蜜蜜麻豆|
久久国产精品久久|
狠狠色婷婷综合天天久久丁香|
亚洲精品tv久久久久久久久
|
99久久国语露脸精品国产|
久久妇女高潮几次MBA|
欧美精品乱码99久久蜜桃|
午夜精品久久久久久久无码|