# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
26260 |
2017-06-28T15:38:56 Z |
sgtlaugh |
Cities (BOI16_cities) |
C++14 |
|
4000 ms |
41656 KB |
#include <stdio.h>
#include <bits/stdtr1c++.h>
#define MAXK 5
#define MAXN 100010
#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)
#define dbg(x) cout << #x << " = " << x << endl
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))
using namespace std;
/*Copyright (c) 2010, Robin Message <[email protected]>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Univsersity of Cambridge nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF CAMBRIDGE OR ROBIN MESSAGE
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
template <class V> class FibonacciHeap;
template <class V> struct node {
private:
node<V>* prev;
node<V>* next;
node<V>* child;
node<V>* parent;
V value;
int degree;
bool marked;
public:
friend class FibonacciHeap<V>;
node<V>* getPrev() {return prev;}
node<V>* getNext() {return next;}
node<V>* getChild() {return child;}
node<V>* getParent() {return parent;}
V getValue() {return value;}
bool isMarked() {return marked;}
bool hasChildren() {return child;}
bool hasParent() {return parent;}
};
template <class V> class FibonacciHeap {
protected:
node<V>* heap;
public:
FibonacciHeap() {
heap=_empty();
}
virtual ~FibonacciHeap() {
if(heap) {
_deleteAll(heap);
}
}
node<V>* insert(V value) {
node<V>* ret=_singleton(value);
heap=_merge(heap,ret);
return ret;
}
void merge(FibonacciHeap& other) {
heap=_merge(heap,other.heap);
other.heap=_empty();
}
bool isEmpty() {
return heap==NULL;
}
V getMinimum() {
return heap->value;
}
V removeMinimum() {
node<V>* old=heap;
heap=_removeMinimum(heap);
V ret=old->value;
delete old;
return ret;
}
void decreaseKey(node<V>* n,V value) {
heap=_decreaseKey(heap,n,value);
}
node<V>* find(V value) {
return _find(heap,value);
}
private:
node<V>* _empty() {
return NULL;
}
node<V>* _singleton(V value) {
node<V>* n=new node<V>;
n->value=value;
n->prev=n->next=n;
n->degree=0;
n->marked=false;
n->child=NULL;
n->parent=NULL;
return n;
}
node<V>* _merge(node<V>* a,node<V>* b) {
if(a==NULL)return b;
if(b==NULL)return a;
if(a->value>b->value) {
node<V>* temp=a;
a=b;
b=temp;
}
node<V>* an=a->next;
node<V>* bp=b->prev;
a->next=b;
b->prev=a;
an->prev=bp;
bp->next=an;
return a;
}
void _deleteAll(node<V>* n) {
if(n!=NULL) {
node<V>* c=n;
do {
node<V>* d=c;
c=c->next;
_deleteAll(d->child);
delete d;
} while(c!=n);
}
}
void _addChild(node<V>* parent,node<V>* child) {
child->prev=child->next=child;
child->parent=parent;
parent->degree++;
parent->child=_merge(parent->child,child);
}
void _unMarkAndUnParentAll(node<V>* n) {
if(n==NULL)return;
node<V>* c=n;
do {
c->marked=false;
c->parent=NULL;
c=c->next;
}while(c!=n);
}
node<V>* _removeMinimum(node<V>* n) {
_unMarkAndUnParentAll(n->child);
if(n->next==n) {
n=n->child;
} else {
n->next->prev=n->prev;
n->prev->next=n->next;
n=_merge(n->next,n->child);
}
if(n==NULL)return n;
node<V>* trees[64]={NULL};
while(true) {
if(trees[n->degree]!=NULL) {
node<V>* t=trees[n->degree];
if(t==n)break;
trees[n->degree]=NULL;
if(n->value<t->value) {
t->prev->next=t->next;
t->next->prev=t->prev;
_addChild(n,t);
} else {
t->prev->next=t->next;
t->next->prev=t->prev;
if(n->next==n) {
t->next=t->prev=t;
_addChild(t,n);
n=t;
} else {
n->prev->next=t;
n->next->prev=t;
t->next=n->next;
t->prev=n->prev;
_addChild(t,n);
n=t;
}
}
continue;
} else {
trees[n->degree]=n;
}
n=n->next;
}
node<V>* min=n;
do {
if(n->value<min->value)min=n;
n=n->next;
} while(n!=n);
return min;
}
node<V>* _cut(node<V>* heap,node<V>* n) {
if(n->next==n) {
n->parent->child=NULL;
} else {
n->next->prev=n->prev;
n->prev->next=n->next;
n->parent->child=n->next;
}
n->next=n->prev=n;
n->marked=false;
return _merge(heap,n);
}
node<V>* _decreaseKey(node<V>* heap,node<V>* n,V value) {
if(n->value<value)return heap;
n->value=value;
if(n->value<n->parent->value) {
heap=_cut(heap,n);
node<V>* parent=n->parent;
n->parent=NULL;
while(parent!=NULL && parent->marked) {
heap=_cut(heap,parent);
n=parent;
parent=n->parent;
n->parent=NULL;
}
if(parent!=NULL && parent->parent!=NULL)parent->marked=true;
}
return heap;
}
node<V>* _find(node<V>* heap,V value) {
node<V>* n=heap;
if(n==NULL)return NULL;
do {
if(n->value==value)return n;
node<V>* ret=_find(n->child,value);
if(ret)return ret;
n=n->next;
}while(n!=heap);
return NULL;
}
};
const long long INF = 1LL << 60;
bitset <MAXN> visited;
int n, k, m, special[MAXK];
long long dis[1 << MAXK][MAXN];
vector <pair<int, int> > graph[MAXN];
void dijkstra(long long dis[MAXN]){
int i, j, v, w;
FibonacciHeap <pair<long long, int> > PQ;
for (i = 1; i <= n; i++){
visited[i] = 0;
if (dis[i] < INF) PQ.insert(make_pair(-dis[i], i));
}
while (!PQ.isEmpty()){
pair <long long, int> cur = PQ.removeMinimum();
if (!visited[cur.second] && -cur.first == dis[cur.second]){
i = cur.second, visited[i] = 1;
for (j = 0; j < graph[i].size(); j++){
v = graph[i][j].first, w = graph[i][j].second;
if (dis[v] > dis[i] + w){
dis[v] = dis[i] + w;
if (!visited[v]) PQ.insert(make_pair(-dis[v], v));
}
}
}
}
}
long long minimumSteinerTree(){
int i, j, mask, submask;
for (mask = 1; mask < (1 << k); mask++){
for (i = 1; i <= n; i++) dis[mask][i] = INF;
if (__builtin_popcount(mask) == 1){
dis[mask][special[31 - __builtin_clz(mask)]] = 0;
}
else{
for (i = 1; i <= n; i++){
for (submask = mask - 1; submask >= (submask ^ mask); submask = (submask - 1) & mask){
dis[mask][i] = min(dis[mask][i], dis[submask][i] + dis[submask ^ mask][i]);
}
}
}
dijkstra(dis[mask]);
}
return *std::min_element(dis[mask - 1] + 1, dis[mask - 1] + 1 + n);
}
int main(){
int i, j, u, v, w;
while (scanf("%d %d %d", &n, &k, &m) != EOF){
for (i = 0; i < MAXN; i++) graph[i].clear();
for (i = 0; i < k; i++) scanf("%d", &special[i]);
for (i = 0; i < m; i++){
scanf("%d %d %d", &u, &v, &w);
graph[u].push_back(make_pair(v, w));
graph[v].push_back(make_pair(u, w));
}
printf("%lld\n", minimumSteinerTree());
}
return 0;
}
Compilation message
cities.cpp: In function 'void dijkstra(long long int*)':
cities.cpp:280:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0; j < graph[i].size(); j++){
^
cities.cpp: In function 'long long int minimumSteinerTree()':
cities.cpp:292:12: warning: unused variable 'j' [-Wunused-variable]
int i, j, mask, submask;
^
cities.cpp: In function 'int main()':
cities.cpp:313:12: warning: unused variable 'j' [-Wunused-variable]
int i, j, u, v, w;
^
cities.cpp:317:57: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (i = 0; i < k; i++) scanf("%d", &special[i]);
^
cities.cpp:320:42: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &u, &v, &w);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
29380 KB |
Output is correct |
2 |
Correct |
0 ms |
29380 KB |
Output is correct |
3 |
Correct |
0 ms |
29380 KB |
Output is correct |
4 |
Correct |
0 ms |
29380 KB |
Output is correct |
5 |
Correct |
0 ms |
29380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1223 ms |
41656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
29512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2799 ms |
41656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4000 ms |
41656 KB |
Execution timed out |
2 |
Halted |
0 ms |
0 KB |
- |