锘??xml version="1.0" encoding="utf-8" standalone="yes"?> Description After a long lasting war on words, a war on arms finally breaks out between littleken’s and KnuthOcean’s kingdoms. A sudden and violent assault by KnuthOcean’s force has rendered a total failure of littleken’s command network. A provisional network must be built immediately. littleken orders snoopy to take charge of the project. With the situation studied to every detail, snoopy believes that the most urgent point is to enable littenken’s commands to reach every disconnected node in the destroyed network and decides on a plan to build a unidirectional communication network. The nodes are distributed on a plane. If littleken’s commands are to be able to be delivered directly from a node A to another node B, a wire will have to be built along the straight line segment connecting the two nodes. Since it’s in wartime, not between all pairs of nodes can wires be built. snoopy wants the plan to require the shortest total length of wires so that the construction can be done very soon. Input The input contains several test cases. Each test case starts with a line containing two integer N (N ≤ 100), the number of nodes in the destroyed network, and M (M ≤ 104), the number of pairs of nodes between which a wire can be built. The next N lines each contain an ordered pair xi and yi, giving the Cartesian coordinates of the nodes. Then follow M lines each containing two integers i and j between 1 and N (inclusive) meaning a wire can be built between node i and node j for unidirectional command delivery from the former to the latter. littleken’s headquarter is always located at node 1. Process to end of file. Output For each test case, output exactly one line containing the shortest total length of wires to two digits past the decimal point. In the cases that such a network does not exist, just output ‘ Sample Input Sample Output Source Input consists of several test cases. The first line of input for each case contains two positive integers: n <= 15, the number of water stations, and m < 1000, the number of trails. For each trail, there is one subsequent line of input containing three positive integers: the first two, between 1 and n, indicating the water stations at the end points of the trail; the third indicates the length of the trail, in cubits. There may be more than one trail between any two stations; each different trail is given only once in the input; each trail can be travelled in either direction. It is possible to reach any trail from any other trail by visiting a sequence of water stations connected by trails. Gord's route may start at any water station, and must end at the same station. A single line containing 0 follows the last test case. For each case, there should be one line of output giving the length of Gord's jogging route. After recent blackouts in some regions in North America, the government has decided to reorganize the power supply network of the continent. The power supply network is the set of nodes, such as power plants or transformation stations, connected by transmission lines. All lines are used to transmit electricity from one node to another. For stability reasons the system is organized in such a way that there are no directed cycles. Since the government is currently short of money due to several small peaceful militaristic operations, it cannot build new power lines for the moment. So after reorganization the same lines will be used, but some lines will have to transmit electricity in the direction opposite to the current one. To make the reorganization gentle enough, the management of the power network is planning to switch the transmission direction for exactly one line each day. Of course, no day there must be a cycle in a network, since this may cause damage to the system. The resulting network is also designed to be acyclic. Help them to plan the reorganization. Input There are mutilple cases in the input file. The first line of the input file contains n --- the number of nodes in the network, and m --- the number of transmission lines (2 <= n <= 1,000 , 1 <= m <= 10,000 ). The following m lines contain three integer numbers each. The first two give the source and the destination node for the corresponding line in the current node. The third number is 0 if the line must keep its transmission direction in the resulting network, and 1 if the direction must be reversed. There can be several lines connecting the same pair of nodes, but due to acyclicity condition, they all transmit electricity in the same direction. This is also the reason why no line connects a node to itself. There is an empty line after each case. Output First output k --- the number of days in the plan you suggest. You don't need to minimize this number, but it must not exceed 4m . After that print k integer numbers --- for each day output the number of the line that changes the transmission direction this day. If it is impossible to make the desired reorganization, output -1 instead of k . There should be an empty line after each case. Sample Input Sample OutputTransfer water
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1770 Accepted Submission(s): 650
First line of each case contains 4 integers n (1<=n<=1000), the number of the households, X (1<=X<=1000), Y (1<=Y<=1000), Z (1<=Z<=1000).
Each of the next n lines contains 3 integers a, b, c means the position of the i‐th households, none of them will exceeded 1000.
Then next n lines describe the relation between the households. The n+i+1‐th line describes the relation of the i‐th household. The line will begin with an integer k, and the next k integers are the household numbers that can build a water line from the i‐th household.
If n=X=Y=Z=0, the input ends, and no output for that.
涓閬撹丹瑁哥殑鏈灝忔爲褰㈠浘錛岄櫎浜嗘暟鎹湁鐐瑰ぇ鑰屽凡.....
鎬濊礬錛氬洜涓烘病鏈夋牴錛屾墍浠ヨ櫄鎷熶竴涓牴錛屾墍鏈夌偣鍜岃繖涓牴榪炵嚎錛屾潈鍊兼槸璇ョ偣閫犱簳鐨勪環鏍鹼紝榪欐牱浠ヨ繖涓牴鍑哄彂錛屾瀯閫犲嚭鏉ョ殑鏈灝忔爲褰㈠浘灝辨槸鏈灝忕殑璐圭敤浜嗐?br />浠g爜錛?br />
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
const int maxn = 1100;
const int maxm = 1100000;
const int maxint = 0x3fffffff;
struct edge
{
int u, v, w;
edge(){}
edge(int u1, int v1, int w1) : u(u1), v(v1), w(w1){}
} e[maxm];
int root, n, edgeNum, vis[maxn], pre[maxn], belong[maxn], in[maxn];
int a[maxn], b[maxn], c[maxn];
int Abs(int a)
{
return a > 0 ? a : -a;
}
int Dis(int i, int j)
{
return Abs(a[i] - a[j]) + Abs(b[i] - b[j]) + Abs(c[i] - c[j]);
}
int solve()
{
int i, j, k, num, sum = 0;
n++;
while (1)
{
for (i = 0; i < n; ++i)
in[i] = maxint;
for (i = 0; i < edgeNum; ++i)
{
if (in[e[i].v] > e[i].w && e[i].u != e[i].v)
{
pre[e[i].v] = e[i].u;
in[e[i].v] = e[i].w;
}
}
memset(vis, -1, sizeof(vis));
memset(belong, -1, sizeof(belong));
in[root] = 0;
for (num = 0, i = 0; i < n; ++i)
{
sum += in[i];
j = i;
while (vis[j] != i && belong[j] == -1 && j != root)
{
vis[j] = i;
j = pre[j];
}
if (vis[j] == i)
{
for (k = pre[j]; k != j; k = pre[k])
belong[k] = num;
belong[j] = num++;
}
}
if (!num) return sum;
for (i = 0; i < n; ++i)
if (belong[i] == -1)
belong[i] = num++;
for (i = 0; i < edgeNum; ++i)
{
int j = e[i].v;
e[i].u = belong[e[i].u];
e[i].v = belong[e[i].v];
e[i].w -= in[j];
}
n = num;
root = belong[root];
}
return sum;
}
int main()
{
int x, y, z, i, j, k, ii, d;
while (scanf("%d%d%d%d", &n, &x, &y, &z) != EOF)
{
if (!n && !x && !y && !z) break;
for (edgeNum = 0, i = 1; i <= n; ++i)
{
scanf("%d%d%d", &a[i], &b[i], &c[i]);
e[edgeNum++] = edge(0, i, c[i] * x);
}
for (i = 1; i <= n; ++i)
{
scanf("%d", &k);
while (k--)
{
scanf("%d", &ii);
if (i == ii) continue;
d = Dis(i, ii);
if (c[i] >= c[ii])
e[edgeNum++] = edge(i, ii, d * y);
else
e[edgeNum++] = edge(i, ii, d * y + z);
}
}
root = 0;
printf("%d\n", solve());
}
return 0;
}
]]>
]]>Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7267 Accepted: 2160 poor snoopy’.4 6
0 6
4 6
0 0
7 20
1 2
1 3
2 3
3 4
3 1
3 2
4 3
0 0
1 0
0 1
1 2
1 3
4 1
2 331.19
poor snoopy
瑁哥殑鏈灝忔爲褰㈠浘錛屾垜灝辨槸涓轟簡媯楠屾ā鏉?...緇撴灉榪橀敊浜?....%.2lf鍦≒OJ涓婄敤G++浼氳帿鍚嶇殑鎸傛帀錛屾墍浠ョ敤C++灝辮浜嗐?br />浠g爜錛?br />
#include <cstring>
#include <cmath>
#include <algorithm>
#define MAXN 110
#define INF 1e30
using namespace std;
inline double sqr(double x)
{
return x * x;
}
struct point
{
double _x, _y;
double dist_with(const point &rh) const
{
return sqrt(sqr(_x - rh._x) + sqr(_y - rh._y));
}
} po[MAXN];
double g[MAXN][MAXN];
int pre[MAXN];
bool is_out[MAXN];
int vis[MAXN], vcnt;
double solve(int n, int root)
{
memset(is_out, false, n);
double ans = 0;
while (1)
{
int i, j, k;
for (i = 0; i < n; ++i)
if (i != root && !is_out[i])
{
pre[i] = i;
for (j = 0; j < n; ++j)
if (!is_out[j] && g[pre[i]][i] > g[j][i])
pre[i] = j;
if (pre[i] == i) throw false;
}
for (i = 0; i < n; ++i)
if (i != root && !is_out[i])
{
j = i;
vis[i] = ++vcnt;
while (vis[pre[j]] != vcnt && pre[j] != root)
{
j = pre[j];
vis[j] = vcnt;
}
if (pre[j] == i)
break;
}
if (i == n)
{
for (j = 0; j < n; ++j)
if (j != root && !is_out[j])
ans += g[pre[j]][j];
break;
}
j = i;
do
{
is_out[j] = true;
ans += g[pre[j]][j];
j = pre[j];
} while (j != i);
for (int j = 0; j < n; ++j)
if (vis[j] == vcnt)
for (int k = 0; k < n; ++k)
if (!is_out[k])
{
if (g[i][k] > g[j][k])
g[i][k] = g[j][k];
if (g[k][i] > g[k][j] - g[pre[j]][j] && g[k][j] != INF)
g[k][i] = g[k][j] - g[pre[j]][j];
}
is_out[i] = false;
}
return ans;
}
int main()
{
int n, m;
while (scanf("%d%d", &n, &m) != EOF)
{
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
g[i][j] = INF;
for (int i = 0; i < n; ++i)
scanf("%lf%lf", &po[i]._x, &po[i]._y);
for (int i = 0; i < m; ++i)
{
int a, b;
scanf("%d%d", &a, &b);
if (a != b)
g[a - 1][b - 1] = po[a - 1].dist_with(po[b - 1]);
}
try
{
printf("%.2lf\n", solve(n, 0));
}
catch (bool)
{
printf("poor snoopy\n");
}
}
return 0;
}
]]>
涓閬撶湅浼煎崥寮堣鐨勯錛屽叾瀹炲氨鏄ā鏉塊鑰屽凡銆?br />
鍒嗘瀽錛?br />騫抽潰涓奛涓偣錛屼袱涓漢杞祦鍙栫偣錛岃屼笖瑙勫畾褰撳墠鍙栫殑鐐瑰拰涓婁竴涓彇鐨勭偣鐨勬浖鍝堥】璺濈瑕佸皬浜嶭錛屾墍浠ワ紝鐐瑰彲浠ョ湅鎴愪袱涓ゆ垚瀵規秷鍘匯?br />榪欎篃灝辮漿鎴愪簡涓鑸浘鍖歸厤錛屽鏋滃瓨鍦ㄥ畬緹庡尮閰嶏紝閭d箞鍚庢墜鐨勪漢鎬誨彲浠ュ彇瀹岋紝濡傛灉涓嶅瓨鍦紝閭d箞鍏堟墜鐨勪漢鍙互鎷夸竴涓绔嬬偣錛岃繖鏍風浜屼釜浜鴻涔堟病鏈夊尮閰嶇偣錛岃涔堝彧鑳芥媶鍒頒竴涓尮閰嶅錛屼絾榪欐牱鍙堥犳垚浜嗗绔嬬偣銆?br />
緗戜笂鏈変釜浜烘嬁浜屽垎鍥懼尮閰嶈繃浜?...鐪嬫潵鏁版嵁寰堝急.....
浠g爜錛?br />
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#define MAXN 450
using namespace std;
int x[MAXN], y[MAXN];
struct Graph
{
bool mat[MAXN + 1][MAXN + 1];
int n;
bool inque[MAXN + 1];
int que[MAXN], head, tail;
int match[MAXN + 1], father[MAXN + 1], base[MAXN + 1];
void init(int _n)
{
n = _n;
for (int i = 1; i <= n; ++i)
{
match[i] = 0;
for (int j = 1; j <= n; ++j)
mat[i][j] = false;
}
}
int pop()
{
return que[head++];
}
void push(int x)
{
que[tail++] = x;
inque[x] = true;
}
void add_edge(int a, int b)
{
mat[a][b] = mat[b][a] = true;
}
int inpath[MAXN + 1];
static int pcnt;
int find_ancestor(int u, int v)
{
++pcnt;
while (u)
{
u = base[u];
inpath[u] = pcnt;
u = father[match[u]];
}
while (true)
{
v = base[v];
if (inpath[v] == pcnt)
return v;
v = father[match[v]];
}
}
int inblossom[MAXN + 1];
static int bcnt;
void reset(int u, int anc)
{
while (u != anc)
{
int v = match[u];
inblossom[base[v]] = bcnt;
inblossom[base[u]] = bcnt;
v = father[v];
if (base[v] != anc) father[v] = match[u];
u = v;
}
}
void contract(int u, int v)
{
int anc = find_ancestor(u, v);
++bcnt;
reset(u, anc);
reset(v, anc);
if (base[u] != anc) father[u] = v;
if (base[v] != anc) father[v] = u;
for (int i = 1; i <= n; ++i)
if (inblossom[base[i]] == bcnt)
{
base[i] = anc;
if (!inque[i]) push(i);
}
}
int find_augment(int start)
{
for (int i = 1; i <= n; ++i)
{
father[i] = 0;
inque[i] = false;
base[i] = i;
}
head = 0, tail = 0, push(start);
while (head < tail)
{
int u = pop();
for (int v = 1; v <= n; ++v)
if (mat[u][v] && base[v] != base[u] && match[v] != u)
{
if (v == start || (match[v] && father[match[v]]))
contract(u, v);
else
{
if (father[v] == 0)
{
if (match[v])
{
push(match[v]);
father[v] = u;
}
else
{
father[v] = u;
return v;
}
}
}
}
}
return 0;
}
void augment(int finish)
{
int u = finish, v, w;
while (u)
{
v = father[u];
w = match[v];
match[u] = v;
match[v] = u;
u = w;
}
}
int graph_max_match()
{
int ans = 0;
for (int i = 1; i <= n; ++i)
if (match[i] == 0)
{
int finish = find_augment(i);
if (finish)
{
augment(finish);
ans += 2;
}
}
return ans;
}
} g;
int Graph :: bcnt = 0, Graph :: pcnt = 0;
int dis(int i, int j, int l)
{
int d;
d = abs(x[i] - x[j]) + abs(y[i] - y[j]);
if (d <= l) return 1;
else return 0;
}
int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
int l;
g.init(n);
for (int i = 1; i <= n; ++i)
scanf("%d%d", &x[i], &y[i]);
scanf("%d", &l);
for (int i = 1; i < n; ++i)
for (int j = i + 1; j <= n; ++j)
{
if (dis(i, j, l)) g.add_edge(i, j);
}
int sum;
sum = g.graph_max_match();
if (sum == n) puts("YES");
else puts("NO");
}
return 0;
}
]]>1099. Work Scheduling
Memory Limit: 16 MBInput
Output
Sample
input output 3
1 2
2 3
1 3
2
1 2
妯℃澘棰?....涓鑸浘鍖歸厤涓昏鐪嬫ā鏉匡紝鍓╀笅鐨勫氨鏄嚜宸盰Y寤哄浘浜?....
妯℃澘錛?br />
#include <cstring>
#include <algorithm>
#include <iostream>
#define MAXN 256
using namespace std;
struct Graph
{
bool mat[MAXN + 1][MAXN + 1];
int n;
bool inque[MAXN + 1];
int que[MAXN], head, tail;
int match[MAXN + 1], father[MAXN + 1], base[MAXN + 1];
void init(int _n)
{
n = _n;
for (int i = 1; i <= n; ++i)
{
match[i] = 0;
for (int j = 1; j <= n; ++j)
mat[i][j] = false;
}
}
int pop()
{
return que[head++];
}
void push(int x)
{
que[tail++] = x;
inque[x] = true;
}
void add_edge(int a, int b)
{
mat[a][b] = mat[b][a] = true;
}
int inpath[MAXN + 1];
static int pcnt;
int find_ancestor(int u, int v)
{
++pcnt;
while (u)
{
u = base[u];
inpath[u] = pcnt;
u = father[match[u]];
}
while (true)
{
v = base[v];
if (inpath[v] == pcnt)
return v;
v = father[match[v]];
}
}
int inblossom[MAXN + 1];
static int bcnt;
void reset(int u, int anc)
{
while (u != anc)
{
int v = match[u];
inblossom[base[v]] = bcnt;
inblossom[base[u]] = bcnt;
v = father[v];
if (base[v] != anc) father[v] = match[u];
u = v;
}
}
void contract(int u, int v)
{
int anc = find_ancestor(u, v);
++bcnt;
reset(u, anc);
reset(v, anc);
if (base[u] != anc) father[u] = v;
if (base[v] != anc) father[v] = u;
for (int i = 1; i <= n; ++i)
if (inblossom[base[i]] == bcnt)
{
base[i] = anc;
if (!inque[i]) push(i);
}
}
int find_augment(int start)
{
for (int i = 1; i <= n; ++i)
{
father[i] = 0;
inque[i] = false;
base[i] = i;
}
head = 0, tail = 0, push(start);
while (head < tail)
{
int u = pop();
for (int v = 1; v <= n; ++v)
if (mat[u][v] && base[v] != base[u] && match[v] != u)
{
if (v == start || (match[v] && father[match[v]]))
contract(u, v);
else
{
if (father[v] == 0)
{
if (match[v])
{
push(match[v]);
father[v] = u;
}
else
{
father[v] = u;
return v;
}
}
}
}
}
return 0;
}
void augment(int finish)
{
int u = finish, v, w;
while (u)
{
v = father[u];
w = match[v];
match[u] = v;
match[v] = u;
u = w;
}
}
int graph_max_match()
{
int ans = 0;
for (int i = 1; i <= n; ++i)
if (match[i] == 0)
{
int finish = find_augment(i);
if (finish)
{
augment(finish);
ans += 2;
}
}
return ans;
}
} g;
int Graph :: bcnt = 0, Graph :: pcnt = 0;
int main()
{
int n;
scanf("%d", &n);
g.init(n);
int a, b;
while (scanf("%d%d", &a, &b) != EOF)
g.add_edge(a, b);
printf("%d\n", g.graph_max_match());
for (int i = 1; i <= n; ++i)
if (g.match[i])
{
printf("%d %d\n", i, g.match[i]);
g.match[g.match[i]] = 0;
}
return 0;
}
]]>
1871: Jogging TrailsResult TIME Limit MEMORY Limit Run Times AC Times JUDGE ![]()
3s 8192K 59 17 Standard Sample Input
4 5
1 2 3
2 3 4
3 4 5
1 4 10
1 3 12
0
Output for Sample Input
41
鍙よ佺殑鍥捐闂錛氫腑鍥介偖閫掑憳闂錛屾浘緇忓湪鏌愪釜鍥捐涔︿笂鐪嬭繃錛屼箣鍚庡氨娌℃湁涔嬪悗浜?.....
1. 鍏堣В閲婁笅"搴?鐨勬蹇? 瀵逛簬鏃犲悜鍥句腑鏌愮粨鐐? 瀹冨拰n鏉¤竟鐩歌繛, 灝辯О瀹冪殑搴︿負n. (鏈夊悜鍥捐繕鍒嗗嚭搴﹀叆搴? 榪欓噷綆鍖栦簡, 涓嶇)
2. 鍙傝冩鎷夊洖璺殑姒傚康, 鏃犲悜鍥懼瓨鍦ㄦ鎷夊洖璺? 褰撳墠浠呭綋鎵鏈夌偣搴︽暟鍧囦負鍋舵暟.
璇佹槑姣旇緝綆鍗? 鍥犱負璧板畬涓鏉″洖璺? 瀵逛簬鎵鏈夌偣鍧囪繘鍘諱竴嬈? 鍑烘潵涓嬈? 鏁? 瀵逛簬浠繪剰鐐圭殑搴︽暟,閮芥槸鎴愬鐨勫湪"娑堣?.
3. 棰樹腑鎵鎻忚堪鐨勫洖璺? 鏈夐噸澶嶇粡榪囨煇杈? 榪欐病鍏崇郴. 鐜板湪鍋囪閭掑憳鎸夐鐩姹傝蛋浜嗕竴鏉℃渶鐭殑鍥炶礬P.
閭d箞, 鎶奝鎵鏈夐噸澶嶇粡榪囩殑杈? 鎷嗗紑. 鍗沖亣璁句笁嬈$粡榪囪竟L, 鍒欐垜浠彲浠ヤ漢涓虹殑鍦ㄥ師鍥句笂澶氬姞涓ゆ潯杈? 瀵逛簬鎵鏈夐噸澶嶇粡榪囩殑杈歸兘榪欐牱鍋? 淇敼鍚庣殑鍥捐涓篏2. (鍘熷浘涓篏)
瀵逛簬G2, 閭掑憳璧扮殑鍥炶礬, 鍗充負姝ゅ浘鐨勬鎷夊洖璺? 榪欐槸閲嶇偣. (鍏跺疄寰堝ソ鎯? 鍥犱負G2涓殑姣忔潯杈歸兘浠呰閭掑憳璧拌繃涓嬈?)
4. 鍘熼棶棰樺氨鍙互杞寲涓? 濡備綍鎶奊娣誨姞涓浜涜竟, 鍙樻垚G2
鑰孏2鎵鏈夌偣搴︽暟鍧囦負鍋舵暟(榪欐槸鑲畾鐨? 鍥犱負G2瀛樺湪嬈ф媺鍥炶礬)
鏁呰漿鍖栦負, 濡備綍鎶奊涓煇浜涜竟澶嶅埗x嬈? 浠庤屾秷鐏璆涓殑濂囩偣(搴︽暟涓哄鏁扮殑鐐?.
5. 鍙兘鏈夌偣鏅? 涓句釜渚嬪瓙
鍥綠鍙湁涓や釜鐐筧鍜宐, 涓鏉¤竟榪炵潃ab, 閭d箞ab鍧囦負濂囩偣, 瑕佹秷鐏帀, 灝辨妸ab涔嬮棿榪炵嚎澶嶅埗涓嬈?鍥犱負浣犱笉鍙兘闅忎究緇欏畠鍔犱竴鏉′笉瀛樺湪鐨?.)
鐒跺悗鍙樻垚涓ゆ潯杈硅繛鐫ab. 浠嶨2鐨勮搴︾湅, 鏄鎷夊浘浜? 瀵瑰簲鎴怗, 灝辨槸鎶婅繖鏉¤竟璧頒簡涓ゆ鑰屽凡.
6. 濂囩偣鎬繪槸鎴愬鐨勮娑堢伃, 灝辯畻涓や釜濂囩偣涓嶄簰閫? 鑰屾槸緇忚繃寰堝鐐規墠鑳借繛閫? 閭d篃瑕佽繛鍑轟竴鏉¤礬寰勬潵, 浠ユ秷鐏繖瀵瑰鐐?
7. 閭e氨濂藉姙浜?鎶婃墍鏈夊鐐規壘鍑烘潵, 鐒跺悗閰嶅, 濡傛灉鏌愮閰嶅鏂瑰紡浠d環鏈灝? 鍗充負鎵姹?
榪欓噷榪樿鐢ㄤ笂floyd, 姹傛渶鐭礬
8. 閰嶅綆楁硶涔熻姣旇緝濂? 瑁哥潃閰嶅ぇ姒備細瓚呮椂, 榪欎釜鎳掑緱璇翠簡.. 濡傛灉涓嶄細, 鐩存帴鐪嬩唬鐮佸惂.
緇欑偣鎵艱娉ㄩ噴, deg璁板綍搴︽暟, mp璁板綍鏈鐭礬. u璁板綍閫掑綊鏃跺悇鐐規槸鍚﹀凡閰嶅. (鍋剁偣鐩存帴琚涓哄凡閰嶅. 閫掑綊鏃跺彧璺繃)
dfs榪斿洖鍦ㄥ綋鍓嶆縐島璁板綍鐨勯厤瀵規儏鍐典笅, 鎶婃湭閰嶅鐨勫尮閰嶅畬, 鏈灝忎唬浠鋒槸澶氬皯
榪樻槸鎻愪竴鍙ュ惂, 姣忔閰嶅鏃? 閰嶅涓殑絎竴涓彲浠ョ洿鎺ュ彇闃熷垪涓殑絎竴涓病鏈夐厤瀵圭殑鍏冪礌(鍏跺疄闅忎究鍙栭兘琛?. 絎簩涓? 寰幆涓嬈″墿涓嬬殑鍗沖彲.
鍥犱負閰嶅鏄笉鍏沖績欏哄簭鐨? 鎵浠ョ涓涓彲浠ユ兂鎬庝箞鍙栭兘琛? 涓轟簡鏂逛究, 灝辯涓涓簡.
浠g爜錛?br />
#include <cstring>
#include <cmath>
#include <iostream>
#define N 17
#define inf 1 << 20
using namespace std;
int deg[N], map[N][N], u[N];
int ans, n, m;
int min(int a, int b)
{
if (a > b) return b;
return a;
}
int slove()
{
int i, j, rnt = inf;
for (i = 0; i < n; ++i)
if (!u[i]) break;
if (i == n) return 0;
u[i] = 1;
for (j = i + 1; j < n; ++j)
{
if (!u[j]) u[j] = 1, rnt = min(rnt, slove() + map[i][j]), u[j] = 0;
}
u[i] = 0;
return rnt;
}
int main()
{
while (scanf("%d", &n) && n)
{
memset(u, 0, sizeof(u));
memset(deg, 0, sizeof(deg));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
map[i][j] = inf;
ans = 0;
scanf("%d", &m);
for (int i = 1; i <= m; ++i)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
ans += c;
a--; b--;
deg[a]++;
deg[b]++;
map[a][b] = map[b][a] = min(map[a][b], c);
}
for (int k = 0; k < n; ++k)
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
{
map[i][j] = min(map[i][j], map[i][k] + map[k][j]);
}
for (int i = 0; i < n; ++i)
if (deg[i] % 2 == 0) u[i] = 1;
printf("%d\n", ans + slove());
}
return 0;
}
]]>4 5
1 2 0
2 3 1
2 4 1
1 4 1
4 3 0
2 2
1 2 1
1 2 1
3
3 2 4
-1
Source: Andrew Stankevich's Contest #9
涓閬撲笉閿欑殑鍥捐棰橈紝澶ф剰鏄粰涓涓湁鍚戠綉緇淣錛屽繀欏誨皢鍏朵腑X鏉¤竟鍙嶅悜錛岀粰鍑轟竴涓柟妗堥『搴忥紝浣垮緱鍦ㄦ墽琛屽弽鍚戠殑榪囩▼涓紝緗戠粶涓嶄細鍑虹幇鐜紝濡傛灉涓嶅瓨鍦ㄨ繖涓柟妗堬紝杈撳嚭-1.
鎬濊礬錛氶鍏堬紝鍒ゆ柇涓嶅瓨鍦ㄧ殑鎯呭喌錛?.涓や釜閲嶈竟閮借鍙嶈漿錛岄偅涔堝湪鎿嶄綔榪囩▼涓偗瀹氬嚭鐜扮幆錛?.鍦ㄥ垵濮嬬綉緇滄垨緇撴潫緗戠粶瀛樺湪鐜傚浜庣浜岀鎯呭喌錛屽彲浠ョ敤topo鎺掑簭姹備竴涓嬩袱涓綉緇滄槸鍚﹀瓨鍦ㄧ幆錛屽茍璁板綍涓媡opp搴忓垪銆?br />涔嬪悗錛屾垜浠鏋勯犳柟妗堬紝鏋勯犳柟妗堢殑榪囩▼鏈変袱涓細
1.涓涓偣鐨勫彲閫嗗叆搴﹁竟錛堣繖鍙ユ槸搴熻瘽錛屽彲閫嗚竟灝辨槸蹇呴』鏀瑰彉鐨勮竟錛岃偗瀹氬寘鍚簬鏂規錛屼絾娉ㄦ剰鏄叆搴︼級
2.榪欎釜鐐瑰繀欏諱粠緇撳熬緗戠粶鐨則opo搴忓垪浠庡悗寰鍓嶆悳绱?....錛堝噷涔變簡鍚?...錛?br />棣栧厛錛屾垜浠鏄庣櫧topo搴忓垪鐨勬ц川錛屽氨鏄簭鍒梐1,a2,a3,...,an錛岃〃紺虹殑鏄綉緇渘涓偣鐨勭嚎鎬у叧緋伙紝瀛樺湪浠繪剰鐨刬<j,浣垮緱ai -> aj錛屼篃灝辨槸錛屽鏋滅敤緗戠粶琛ㄧずtopo搴忓垪錛岄偅涔堝彧鏈夊線鍙寵竟鎸囧悜鐨勮竟銆?br />閫氳繃榪欎竴涓ц川錛屽姞涓婄綉緇淣鍓嶅悗涓ゆ鐨則opo搴忓垪錛屾垜浠笉闅懼彂鐜幫紝緇撳熬緗戠粶topo搴忓垪鐨勬渶鍚庝竴涓偗瀹氭槸鍦ㄦ搷浣滀腑澶卞幓鍑哄害鑰屼粠鍒濆鐨則opo搴忓垪闄嶄負錛堟垨鍋滅暀錛夋渶鍚庨潰錛屾墍浠ワ紝灝嗗叾鍙弽杞殑鍏ュ害杈瑰弽鍚戯紝鑲畾涓嶄細瀛樺湪鍥炲皠杈逛粠鑰屼駭鐢熺幆緇撴瀯錛屽洜姝わ紝浠庢渶鍚庝竴涓偣鍚戝墠鎼滅儲錛屾瘡嬈℃墽琛屽彲鎵ц鐨勫弽杞搷浣滐紝閭d箞涓瀹氳兘淇濇寔褰撳墠鐐圭殑涓嶅瓨鍦ㄥ洖灝勮竟銆?br />瀵逛簬涓涓偣錛屽彲瀛樺湪鍚屾椂鏀厤澶氭潯鍙弽杞竟錛屽洜涓虹瓟妗堣姹備竴嬈′竴嬈℃墽琛屽弽杞紝濡傛灉瀵逛簬鍚屼竴涓偣欏哄簭涓嶅綋鍙兘鍑虹幇鐜紝鎵浠ユ垜浠冭檻浠ヤ笅闂錛歺->y, x->z,濡傛灉鍙嶈漿錛坸,y錛変粠鑰屽鑷翠簡鐜殑鍑虹幇錛岄偅涔堝彲浠ヨ偗瀹歾->y鏄垚绔嬬殑錛岃屽湪topo鍏崇郴涓妟姣攜闈犲墠錛屾墍浠ユ垜浠浜庡悓涓涓偣杈撳嚭緇撴灉鏃訛紝瑕佹寜鐓у叾鍒濆緗戠粶鐨則opo欏哄簭錛屼粠宸﹀悜鍙寵緭鍑恒?br />鎬濊礬瀹屾瘯錛孉C錛岃瘉鏄?...鐣ヤ簡鍚э紝鎴戣瘉鏄庝簡涓鑽夌ǹ綰搞?br />娉ㄦ剰涓嶈鐢ㄧ煩闃碉紝鎴戝洜涓洪偅涓悆浜嗗嚑嬈E......
浠g爜錛?br />
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define N 1100
#define M 10010
using namespace std;
int n, m;
struct edge
{
int u, v, next;
} et[2][M];
int eh[2][N], tot[2];
int be[2 * N], ed[2 * N], sta[2 * N];
int deg[2][N], deg2[N];
int g[N][N];
void add(int u, int v, int i)
{
int t = ++tot[i];
et[i][t].u = u;
et[i][t].v = v;
et[i][t].next = eh[i][u];
eh[i][u] = t;
deg[i][v]++;
}
int topo(int eh[], edge et[], int que[], int deg[])
{
int i, j, k, top = -1, qt = 0;
for (i = 1; i <= n; ++i)
deg2[i] = deg[i];
for (i = 1; i <= n; ++i)
if (deg2[i] == 0)
sta[++top] = i;
for (j = 1; j <= n; ++j)
{
if (top == -1) return 0;
int u = sta[top--];
que[qt++] = u;
for (i = eh[u]; i != -1; i = et[i].next)
{
deg2[et[i].v]--;
if (deg2[et[i].v] == 0) sta[++top] = et[i].v;
}
}
return 1;
}
int was[M], cnt;
void slove()
{
int i, j, k;
memset(was, 0, sizeof(was));
printf("%d\n", cnt);
for (i = n - 1; i >= 0; --i)
{
int u = ed[i];
for (j = 0; j < n; ++j)
if (g[u][be[j]] > 0)
{
if (was[g[u][be[j]]] == 0)
{
was[g[u][be[j]]] = 1;
printf("%d ", g[u][be[j]]);
}
}
}
if (cnt > 0) printf("\n");
}
int main()
{
int i, j, k;
while (scanf("%d%d", &n, &m) != EOF)
{
memset(eh, -1, sizeof(eh));
memset(deg, 0, sizeof(deg));
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j)
g[i][j] = 0;
tot[1] = tot[0] = 0;
cnt = 0;
int flag = 1;
for (i = 1; i <= m; ++i)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, 0);
if (c)
{
add(b, a, 1);
cnt++;
if (g[a][b] > 0) flag = 0;
g[a][b] = i;
}
else
add(a, b, 1);
}
if (flag == 0)
{
printf("-1\n\n");
continue;
}
int first = topo(eh[0], et[0], be, deg[0]);
int last = topo(eh[1], et[1], ed, deg[1]);
if (!(first && last))
{
printf("-1\n\n");
continue;
}
slove();
printf("\n");
}
return 0;
}
]]>