锘??xml version="1.0" encoding="utf-8" standalone="yes"?> Description Farmer
John has decided to reward his cows for their hard work by taking them
on a tour of the big city! The cows must decide how best to spend their
free time. Fortunately, they have a detailed city map showing the L (2 ≤ L ≤ 1000) major landmarks (conveniently numbered 1.. L) and the P (2 ≤ P
≤ 5000) unidirectional cow paths that join them. Farmer John will drive
the cows to a starting landmark of their choice, from which they will
walk along the cow paths to a series of other landmarks, ending back at
their starting landmark where Farmer John will pick them up and take
them back to the farm. Because space in the city is at a premium, the
cow paths are very narrow and so travel along each cow path is only
allowed in one fixed direction. While the cows may spend as much
time as they like in the city, they do tend to get bored easily.
Visiting each new landmark is fun, but walking between them takes time.
The cows know the exact fun values Fi (1 ≤ Fi ≤ 1000) for each landmark i. The cows also know about the cowpaths. Cowpath i connects landmark L1i to L2i (in the direction L1i -> L2i ) and requires time Ti (1 ≤ Ti ≤ 1000) to traverse. In
order to have the best possible day off, the cows want to maximize the
average fun value per unit time of their trip. Of course, the landmarks
are only fun the first time they are visited; the cows may pass through
the landmark more than once, but they do not perceive its fun value
again. Furthermore, Farmer John is making the cows visit at least two
landmarks, so that they get some exercise during their day off. Help the cows find the maximum fun value per unit time that they can achieve. Input * Line 1: Two space-separated integers: L and P Output *
Line 1: A single number given to two decimal places (do not perform
explicit rounding), the maximum possible average fun per unit time, or
0 if the cows cannot plan any trip at all in accordance with the above
rules. Sample Input Sample Output Description For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race using the T (2 ≤ T ≤ 100) cow trails throughout the pasture. Each trail connects two different intersections (1 ≤ I1i ≤ 1,000; 1 ≤ I2i ≤ 1,000), each of which is the termination for at least two trails. The cows know the lengthi of each trail (1 ≤ lengthi
≤ 1,000), the two intersections the trail connects, and they know that
no two intersections are directly connected by two different trails.
The trails form a structure known mathematically as a graph. To run the relay, the N
cows position themselves at various intersections (some intersections
might have more than one cow). They must position themselves properly
so that they can hand off the baton cow-by-cow and end up at the proper
finishing place. Write a program to help position the cows. Find the shortest path that connects the starting intersection (S) and the ending intersection (E) and traverses exactly N cow trails. Input * Line 1: Four space-separated integers: N, T, S, and E Output * Line 1: A single integer that is the shortest distance from intersection S to intersection E that traverses exactly N cow trails. Sample Input Sample Output Description
The tree can be represented as a collection of numbered nodes and some edges. The nodes are numbered 1 through n.
The root is always numbered 1. Every node in the tree has its weight.
The weights can be different from each other. Also the shape of every
available edge between two nodes is different, so the unit price of
each edge is different. Because of a technical difficulty, price of an
edge will be (sum of weights of all descendant nodes) × (unit price of
the edge). Suby wants to minimize the cost of whole tree among
all possible choices. Also he wants to use all nodes because he wants a
large tree. So he decided to ask you for helping solve this task by
find the minimum cost. Input The input consists of T test cases. The number of test cases T is given in the first line of the input file. Each test case consists of several lines. Two numbers v, e (0 ≤ v, e ≤ 50000) are given in the first line of each test case. On the next line, v positive integers wi indicating the weights of v nodes are given in one line. On the following e lines, each line contain three positive integers a, b, c indicating the edge which is able to connect two nodes a and b, and unit price c. All numbers in input are less than 216. Output For
each test case, output an integer indicating the minimum possible cost
for the tree in one line. If there is no way to build a Christmas tree,
print “No Answer” in one line. Sample Input Sample Output
姣忎釜mst涓殑瀛恗st鍙兘鏋勬垚涓嶅悓錛屼絾鍊間竴瀹氫笉鍙橈紝閭d箞鏂拌竟e[u,v]鐨勫姞鍏ワ紝灝唌st鍒嗘垚涓夐儴鍒嗭紝浠璺焩涔嬮棿鍓蹭負涓浠組錛宮st鍘繪帀M鍚庝互u涓烘牴鐨刄錛屽拰浠涓烘牴鐨刅銆?br>e[u,v]瀵筓璺烿娌″獎鍝嶏紝鍙獎鍝嶄簡M錛圡璺焑鑲畾鏋勬垚鐜疕錛夛紝鎵浠ュ彧闇瑕佸湪H涓鎵炬渶澶х殑杈瑰垹闄ゅ嵆鍙?br>
#include <stdlib.h>
#define inf 1000000000
#define maxn 1010
int dis[maxn], visit[maxn], map[maxn][maxn], path[maxn], hash[maxn][maxn];
int n, flag, th;
struct T
{
int u, v, w, next;
}fn[maxn * maxn];
struct R
{
int v, w;
}p[maxn];
int g[maxn];
void Set()
{
int i, j;
for (i = 0; i <= n; i++)
{
dis[i] = inf, visit[i] = 0, path[i] = 0, g[i] = -1;
}
}
//鏃犲悜鍥鵑偦鎺ヨ〃鍔犺竟
void add(int u, int v, int w)
{
fn[th].u = u, fn[th].v = v, fn[th].next = g[u], fn[th].w = w, g[u] = th++;
fn[th].u = v, fn[th].v = u, fn[th].next = g[v], fn[th].w = w, g[v] = th++;
}
//鏃犲悜鍥鵑偦鎺ヨ〃鍒犺竟
void del(int u, int v)
{
int i, j;
i = g[u];
if (i != -1)
{
if (fn[i].v == v)
{
g[u] = fn[i].next;
}
else
{
for (j = fn[i].next; j != -1; j = fn[j].next, i = fn[i].next)
{
if (fn[j].v == v)
{
fn[i].next = fn[j].next;
break;
}
}
}
}
i = g[v];
if (i != -1)
{
if (fn[i].v == u)
{
g[v] = fn[i].next;
}
else
{
for (j = fn[i].next; j != -1; j = fn[j].next, i = fn[i].next)
{
if (fn[j].v == u)
{
fn[i].next = fn[j].next;
break;
}
}
}
}
}
//閭繪帴鐭╅樀鏅埄濮?nbsp;
int Prime()
{
Set();
int i, j, pre, next, sum = 0, min;
for (i = 0, pre = 1; i < n - 1; i++)
{
visit[pre] = 1, min = inf;
for (j = 1; j <= n; j++)
{
if (visit[j] == 0)
{
if (dis[j] > map[pre][j])
{
dis[j] = map[pre][j];
path[j] = pre;//鏇存柊鎴栦繚瀛榡鍦╩st涓殑鐖惰妭鐐?nbsp;
}
if (min > dis[j])
{
min = dis[j];
next = j;
}
}
}
pre = next;
sum += dis[pre];
}
for (i = 1; i <= n; i++)
{
if (path[i])//hash鐢ㄦ潵鍒ゆ柇hash[i][j]鏄惁鏄痬st鐨勮竟
{
hash[i][path[i]] = hash[path[i]][i] = 1;
}
}
for (i = 1; i <= n; i++)//灝唌st鐢ㄩ偦鎺ヨ〃閲嶆柊鏋勫浘
{
for (j = 1; j < i; j++)
{
if (hash[i][j])
{
add(i, j, map[i][j]);
}
}
}
return sum;
}
inline void dfs(int u)
{
visit[u] = 1;
int i;
for (i = 1; i <= n; i++)
{
if (!visit[i] && map[u][i] != inf)
{
dfs(i);
}
}
}
void find(int u, int f)//閭繪帴琛╩st涓悳瀵籾鍒癴涔嬮棿鐨勮竟
{
visit[u] = 1;
if (u == f)
{
flag = 1;
return;
}
int i, v;
for (i = g[u]; i != -1; i = fn[i].next)
{
v = fn[i].v;
if (!visit[v])
{
p[v].v = u, p[v].w = fn[i].w;
find(v, f);
if (flag)
{
return;
}
}
}
}
void show()
{
int i, j;
for (i = 1; i <= n; i++)
{
printf("%d:", i);
for (j = g[i]; j != -1; j = fn[j].next)
{
printf(" [%d,%d]", fn[j].v, fn[j].w);
}
printf("\n");
}
}
int main()
{
//freopen("net.in", "r", stdin);
//freopen("ex.out", "w", stdout);
int t, m, i, j, sign, u, v, w, sum, ca = 0;
scanf("%d", &t);
while (t--)
{
ca++;
th = 0;
printf("Case %d:\n", ca);
scanf("%d%d", &n, &m);
sign = 1;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
{
map[i][j] = inf, hash[i][j] = 0;
}
while (m--)
{
scanf("%d%d%d", &u, &v, &w);
if (sign)//鍥炬病榪炲悓鏃訛紝緇х畫鍔犺竟騫跺垽鏂姞杈瑰悗鏄惁榪炲悓錛屽鏋滄槸錛屾眰mst騫剁敤閭繪帴琛ㄩ噸鏋刴st
{
if (map[u][v] > w)
{
map[u][v] = map[v][u] = w;
for (i = 1; i <= n; i++) visit[i] = 0;
dfs(1);
for (i = 1; i <= n && visit[i]; i++);
if (i == n + 1)
{
sign = 0;
sum = Prime();
}
}
if (sign)
{
printf("The net still undone\n");
}
else
{
printf("%d\n", sum);
}
}
else//鍥懼凡榪炲悓錛屽垯瀹屽悗鎵鏈夊姞杈癸紝鍥鵑兘鏄繛鍚岀殑錛屾爲璇鵑殢鐫鍔犲叆鐨勮竟璋冩暣錛屾槸mst鐨勫兼洿灝?nbsp;
{
flag = 0;
for (i = 1; i <= n; i++)
{
visit[i] = 0, p[i].v = -1;
}
find(u, v);
for (i = v, j = v; i != u; i = p[i].v) //鍦╱璺焩鐩存帴瀵繪壘鏈澶ц竟銆傚洜涓虹敤find(u,v),鎵浠鐨勫墠椹辮妭鐐逛負path[i].v
{
if (p[j].w < p[i].w)
{
j = i;
}
}
if (p[j].w > w)
{
sum = sum - p[j].w + w;
del(j, p[j].v);
add(u, v, w);
}
printf("%d\n", sum);
}
}
}
return 0;
}
/*
10
4 10
1 2 1
2 3 3
1 3 1
3 4 1
2 3 3
2 3 0
5 6
1 2 5
2 3 1
2 4 4
2 5 3
1 2 3
1 4 1
*/
]]>
* Lines 2..L+1: Line i+1 contains a single one integer: Fi
* Lines L+2..L+P+1: Line L+i+1 describes cow path i with three space-separated integers: L1i , L2i , and Ti5 7
30
10
10
5
10
1 2 3
2 3 2
3 4 5
3 5 2
4 5 5
5 1 3
5 2 26.00
棰樻剰錛氭瘡鐐規潈val璺熻竟鏉僿錛屼竴鏉¤礬寰勪笂鐨勪紭涔愬間負鐐規潈鍜?杈規潈鍜屻傛眰鐗涗粠涓鐐瑰嚭鍙戝埌鏈鍚庡洖鍒板師鐐圭殑鏈澶т紭涔愬?br>鍒嗘瀽錛氫簩鍒嗗弬鏁癮ns錛宎ns * w1 + ans * w2 + ... + ans * wi <= val1 + val2 + ... + vali;鐢╯pfa鍒ゆ柇緇欏嚭鐨刟ns鑳藉惁鏋勬垚
璐熺幆銆傛湁璇存槑ans灝忎簡錛屾病鏈夎鏄巃ns澶т簡銆?br>浠g爜錛?br>
#include <stdlib.h>
#include <queue>
#include <vector>
#define maxn 1005
#define inf 100000000
#define exp 0.00001
using namespace std;
int visit[maxn], val[maxn];
int cnt[maxn];
double dis[maxn];
struct T
{
int v, w;
}e;
vector <T> g[maxn];
void set(int n)
{
for (int i = 1; i <= n; i++)
{
g[i].clear();
}
}
int spfa(int n, int s, double ans)
{
int head = 0, tial = 1, i, u, v, w, size;
double dd;
queue<int> q;
dis[s] = 0;
cnt[s] = 1;
q.push(s);
while (!q.empty())
{
u = q.front();
q.pop();
visit[u] = 0;
size = g[u].size();
for (i = 0; i < size; i++)
{
v = g[u][i].v, w = g[u][i].w;
dd = dis[u] + ans * w - val[v];
if (dis[v] > dd)
{
dis[v] = dd;
if (!visit[v])
{
cnt[v]++;
if (cnt[v] > n)
{
return 0;
}
tial = (tial + 1) % maxn;
q.push(v);
visit[v] = 1;
}
}
}
}
return 1;
}
int main()
{
int n, m, i, u, v, w, th, sta;
double l, r, mid;
while (scanf("%d%d", &n, &m) != EOF)
{
set(n);
for (i = 1, r = 0; i <= n; i++)
{
scanf("%d", &val[i]);
r += val[i];
}
th = 0;
T node;
while (m--)
{
scanf("%d%d%d", &u, &v, &w);
node.v = v, node.w = w;
g[u].push_back(node);
}
l = 0;
while (l + exp < r)
{
mid = (l + r) / 2.0;
for (i = 1; i <= n; i++)
{
dis[i] = inf;
visit[i] = 0;
cnt[i] = 0;
}
sta = spfa(n, 1, mid);
if (sta == 0)
{
l = mid + exp;
}
else
{
r = mid - exp;
}
}
printf("%.2lf\n", l);
}
return 0;
}
]]>
* Lines 2..T+1: Line i+1 describes trail i with three space-separated integers: lengthi , I1i , and I2i2 6 6 4
11 4 6
4 4 8
8 4 9
6 6 8
2 6 9
3 8 910
棰樻剰錛氭眰緇忚繃n鏉¤礬鐨勬渶鐭礬銆?br>
#include <stdlib.h>
#define inf 1 << 30
#define maxn 101
#define Min(a, b) a < b ? a : b
int hash[maxn*10], g[maxn], num, ksp[23][maxn][maxn], len[maxn][2];
void set()
{
int i;
num = 0;
for (i = 1; i < maxn; i++)
{
hash[i] = 0;
}
}
int makeHash(int a)
{
if (!hash[a])
{
hash[a] = ++num;
}
return hash[a];
}
int main()
{
int n, t, s, e, h, i, j, k, u, v, w;
while (scanf("%d%d%d%d", &n, &t, &s, &e) != EOF)
{
set();
for (h = 0; h < 23; h++)
{
for (i = 1; i < maxn; i++)
{
for (j = 1; j < maxn; j++)
{
ksp[h][i][j] = inf;
}
}
}
while (t--)
{
scanf("%d%d%d", &w, &u, &v);
u = makeHash(u), v = makeHash(v);
ksp[0][u][v] = ksp[0][v][u] = Min(w, ksp[0][v][u]);
}
s = makeHash(s), e = makeHash(e);
for (h = 1; (1 << h) <= n; h++)//姹傚嚭i鍒癹緇忚繃2鐨刪嬈℃柟璺緞鐨勬渶鐭礬
{
for (i = 1; i <= num; i++)
{
for (j = 1; j <= num; j++)
{
if (ksp[h-1][i][j] < inf)
{
for(k = 1; k <= num; k++)
{
if (ksp[h][i][k] > ksp[h-1][i][j] + ksp[h-1][j][k])
{
ksp[h][i][k] = ksp[h-1][i][j] + ksp[h-1][j][k];
}
}
}
}
}
}
for (i = 1; i <= num; i++)
{
len[i][0] = inf;
}
len[s][0] = 0;
for (h = 0, k = 0; (1 << h) <= n; h++)//緇勫悎s鍒板叾浠栬妭鐐圭粡榪噉鏉¤礬鐨勬渶鐭礬
{
if (n & (1 << h))//鏋氫婦n鐨勪簩榪涘埗涓婃湁1鐨勩?nbsp;
{
for (i = 1; i <= num; i++)
{
len[i][k ^ 1] = inf;
}
for (i = 1; i <= num; i++)
{
if (len[i][k] < inf)//榪欓噷瑕佸垽鏂竴涓嬶紝鍥犱負1<<30鍐嶅姞涓?<<30灝辮秴鍑篿nt浜?nbsp;
{
for (j = 1; j <= num; j++)
{
len[j][k ^ 1] = Min(len[j][k ^1], len[i][k] + ksp[h][i][j]);
}
}
}
k = k ^ 1;
}
}
printf("%d\n", len[e][k]);
}
system("pause");
return 0;
}
]]>
Christmas
is coming to KCM city. Suby the loyal civilian in KCM city is preparing
a big neat Christmas tree. The simple structure of the tree is shown in
right picture.2
2 1
1 1
1 2 15
7 7
200 10 20 30 40 50 60
1 2 1
2 3 3
2 4 2
3 5 4
3 7 2
3 6 3
1 5 915
1210
棰樻剰錛氫竴涓竟鍦ㄦ爲涓殑浠d環鏄畠鐨勫瓙鏍戜腑鎵鏈夎妭鐐規潈鍊肩殑鍜屼箻浠ヨ杈圭殑鏉冨箋傛眰榪炴帴鎵鏈夌偣鐨勬渶灝忎唬浠楓?br>鍒嗘瀽錛氭瘡涓偣鍒版牴璐$尞浜嗚礬寰勭殑鐨勬潈鍊煎拰涔樹互鏀圭偣鏉冨箋傝繖灝辨垚浜嗘渶鐭礬鐨勯浜嗐?br>鏈潵浠ヤ負鐢ㄤ釜鏍堝瓨鐐瑰氨琛岋紝娌℃兂鍒拌秴鏃躲?br>鏈鍚庢妸瀹冩敼涓哄驚鐜槦鍒椼?br>
#include <stdlib.h>
#define maxn 70001
#define Min(a, b) a < b ? a : b
long long inf = (long long)1 << 62;
struct
{
int v, next, w;
}edge[maxn * 2];
int g[maxn], visit[maxn], stack[maxn * 2], val[maxn];
long long dis[maxn];
void set(int n)
{
for (int i = 1; i <= n; i++)
{
g[i] = -1;
}
}
void spfa(int n, int s)
{
int i, u, w, v;
for (i = 1; i <= n; i++)
{
dis[i] = inf, visit[i] = 0;
}
int top = 1, head = 0;
stack[top] = s;
dis[s] = 0;
while (head != top)
{
head = (head + 1) % maxn;
u = stack[head];
visit[u] = 0;
for (i = g[u]; i != -1; i = edge[i].next)
{
v = edge[i].v, w = edge[i].w;
if (dis[v] > dis[u] + w)
{
dis[v] = dis[u] + w;
if (!visit[v])
{
top = (top + 1) % maxn;
stack[top] = v;
visit[v] = 1;
}
}
}
}
long long ans;
for (i = 1, ans = 0; i <= n; i++)
{
if (dis[i] == inf)
{
break;
}
ans += dis[i] * val[i];
}
if (i != n + 1)
{
printf("No Answer\n");
}
else
{
printf("%lld\n", ans);
}
}
int main()
{
int t, n, m, i, th, u, v, w, min;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
set(n);
g[1] = -1;//澶勭悊鐐規暟涓?鏃剁殑鍒濆鍖栦竴涓牴錛屾病鍒濆鍖栦細瓚呮椂
for (i = 1; i <= n; i++)
{
scanf("%d", &val[i]);
}
th = 0;
while (m--)
{
scanf("%d%d%d", &u, &v, &w);
edge[th].v = v, edge[th].w = w, edge[th].next = g[u], g[u] = th++;
edge[th].v = u, edge[th].w = w, edge[th].next = g[v], g[v] = th++;
}
spfa(n, 1);
}
system("pause");
return 0;
}
]]>