Posted on 2007-01-02 16:10
oyjpart 阅读(1586)
评论(2) 编辑 收藏 引用 所属分类:
ACM/ICPC或其他比赛
Invitation Cards
Time Limit:3000MS Memory Limit:65536K
Total Submit:241 Accepted:93
Description
In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery.
The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where 'X' denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan.
All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees.
Input
The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= P,Q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop.
Output
For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.
Sample Input
2
2 2
1 2 13
2 1 33
4 6
1 2 10
2 1 60
1 3 20
3 4 10
2 4 5
4 1 50
Sample Output
46
210
Source
Central Europe 1998
这个题目我想就是专考Dijkstra()的堆写法吧 中间我采用的逆转有向边的写法 可以把从多点到单源的最短路径用单源到多点的最短路径的方法求出。
中间出了一个很隐蔽的错误 在一个i, j的双层循环中 内层循环写成了i++...结果调了很久。。。
总算过了 发现STL占内存是直接写邻接表的2倍左右,这也印证了vector的扩张方式。
1
Solution:
2
//
by Optimistic
3
#include
<
stdio.h
>
4
#include
<
string
.h
>
5
#include
<
vector
>
6
using
namespace
std;
7
8
const
int
MAXINT
=
200000000
;
9
//
const double INF = 10e100;
10
//
const double EPS = 10e-6;
11
12
const
int
N
=
1000010
;
13
int
nv, ne;
14
typedef
struct
{
int
jj, w;}
Vtx;
15
vector
<
Vtx
>
adj[N];
16
vector
<
Vtx
>
adj2[N];
17
typedef
struct
{
int
k, no;}
hNode;
18
int
ntc, hs;
19
hNode h[N];
20
21
bool
operator
<
(
const
hNode
&
a,
const
hNode
&
b)
22
{
23
return
a.k
<
b.k;
24
}
25
26
void
push(hNode t)
27
{
28
int
i
=
++
hs;
29
while
(i
>
1
&&
t
<
h[i
>>
1
])
{
30
h[i]
=
h[i
>>
1
];
31
i
>>=
1
;
32
}
33
h[i]
=
t;
34
}
35
36
void
pop()
37
{
38
hs
--
;
39
int
i
=
1
, ic
=
2
;
40
while
(ic
<=
hs)
{
41
if
(ic
+
1
<=
hs
&&
h[ic
+
1
]
<
h[ic]) ic
++
;
42
if
(h[hs
+
1
]
<
h[ic])
break
;
43
h[i]
=
h[ic];
44
i
=
ic;
45
ic
<<=
1
;
46
}
47
h[i]
=
h[hs
+
1
];
48
}
49
50
int
Dijkstra()
51
{
52
hs
=
0
;
53
int
i;
54
int
*
dist
=
new
int
[nv];
55
for
(i
=
0
; i
<
nv; i
++
) dist[i]
=
MAXINT;
56
hNode now;
57
now.k
=
0
; now.no
=
0
;
58
push(now);
59
while
(
1
)
60
{
61
while
(hs
>
0
&&
h[
1
].k
>
dist[h[
1
].no])
62
pop();
63
if
(hs
==
0
)
break
;
64
now
=
h[
1
];
65
pop();
66
int
u
=
now.no;
67
dist[u]
=
now.k;
68
for
(i
=
0
; i
<
adj[u].size(); i
++
)
69
{
70
int
v
=
adj[u][i].jj;
71
int
w
=
adj[u][i].w;
72
if
(dist[v]
>
dist[u]
+
w)
73
{
74
now.k
=
dist[u]
+
w;
75
now.no
=
v;
76
push(now);
77
dist[v]
=
dist[u]
+
w;
78
}
79
}
80
}
81
int
ans
=
0
;
82
for
(i
=
0
; i
<
nv; i
++
)
83
ans
+=
dist[i];
84
return
ans;
85
}
86
87
void
init()
88
{
89
int
i, u, v, w;
90
Vtx x;
91
//
initiation
92
for
(i
=
0
; i
<
nv; i
++
)
{
93
adj2[i].clear();
94
adj[i].clear();
95
}
96
//
input
97
scanf(
"
%d %d
"
,
&
nv,
&
ne);
98
for
(i
=
0
; i
<
ne; i
++
)
{
99
scanf(
"
%d %d %d
"
,
&
u,
&
v,
&
w);
100
u
--
; v
--
;
101
x.jj
=
v; x.w
=
w;
102
adj[u].push_back(x);
103
}
104
//
pretreatment
105
}
106
107
void
Reverse()
108
{
109
int
i, j;
110
Vtx x;
111
for
(i
=
0
; i
<
nv; i
++
)
112
adj2[i].clear();
113
for
(i
=
0
; i
<
nv; i
++
)
114
{
115
for
(j
=
0
; j
<
adj[i].size(); j
++
)
116
{
117
x.jj
=
i;
118
x.w
=
adj[i][j].w;
119
adj2[adj[i][j].jj].push_back(x);
120
}
121
}
122
for
(i
=
0
; i
<
nv; i
++
)
123
{
124
adj[i].clear();
125
for
(j
=
0
; j
<
adj2[i].size(); j
++
)
126
adj[i].push_back(adj2[i][j]);
127
}
128
}
129
130
void
work()
131
{
132
int
x
=
Dijkstra();
133
Reverse();
134
x
+=
Dijkstra();
135
printf(
"
%d\n
"
, x);
136
}
137
138
int
main()
139
{
140
//
freopen("t.in", "r", stdin);
141
scanf(
"
%d
"
,
&
ntc);
142
while
(ntc
--
)
143
{
144
init();
145
work();
146
}
147
return
0
;
148
}
149