Aviamachinations

Time Limit: 4500ms
Memory Limit: 65536KB

This problem will be judged on SGU. Original ID: 323
64-bit integer IO format: %I64d      Java class name: Solution

 
Berland consists of N towns as you probably already know. Berland also has M domestic airlines. In fact all these airlines belong to Don Berlione. Don Berlione was forced to create a number of companies instead of just one by the Antimonopoly Committee.

The Antimonopoly Committee was disbanded as a result of a government crisis. So, Don Berlione decided to close all but one airline. Naturally, this company should have flights (possibly including stopovers) from any town of Berland to any other one. To be able to choose the airline satisfying the above requirement, Don Berlione decided to carry out a number of fake purchase-sell operations. During a purchase-sell operation a flight of one airline is passed under the control of another airline. A purchase-sell operation is just a money transfer from one pocket to another. But still a special tax should be paid to the government for each operation.

So each flight is characterized by two towns it connects, the airline it belongs to and the tax amount that should be paid for a purchase-sell operation.

Your task is to find P — the minimum possible amount of money Don Berlione needs to spend to make it possible to leave only one airline carrying out flights (possibly with stopovers) from each town of Berland to any other. Also you need to suggest a plan of actions for Don Berlione.

 

Input

The first line of the input file contains three integer numbers NMK (1 ≤ N ≤ 2000; 1 ≤ M ≤ 2000; 0 ≤ K ≤ 200000), where N is the number of towns, M is the number of airlines, K is the number of flights. Each of the following K lines contains the description of the flight given by four integer numbers aibicipi, where aibi (ai != bi; 1≤ aibi≤ N) are the numbers of towns connected by the flight (towns are numbered from 1 to N), ci (1≤ ci≤ M) is the number of the airline owning the flight (airlines are numbered from 1 to M), pi (1≤ pi≤ 100000) is the tax amount required for the purchase-sell operation of the flight. Originally all flights are planned in such a way that it is possible to get from each town to any other using flights of one or several airlines. There can be several flights between a pair of towns.

 

Output

Write the desired minimum amount of money P to the first line of the output. After that write a pair of numbers R and Q to the same line, where R is the index of an airline which should be chosen by Don and Q is the number of purchase-sell operations. Write the description of operations to the following Q lines. Each operation should be characterized by a single integer number idxj, which means that the flight idxj should be sold to the company R. If there are several solutions for the problem, choose any of them.

 

Sample Input

Example(s)
sample input
sample output
4 3 4
2 3 1 6
4 3 2 7
1 2 2 3
1 3 3 5
5 2 1
4

解题:最小生成树的妙用啊。。。
 通过枚举航空公司,判断是否连通,不连通,用最小生成树中的边补连通。。。妙哉
 
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
class ARC {
public:
int u,v;
ARC(int x = ,int y = ) {
u = x;
v = y;
}
};
class ARC1:public ARC {
public:
int w,id;
ARC1(int x = ,int y = ,int cw = ,int cid = ):ARC(x,y) {
w = cw;
id = cid;
}
bool operator<(const ARC1 &t) {
return w < t.w;
}
} g[maxn];
class ARC2:public ARC {
public:
int next;
ARC2(int x = ,int y = ,int nxt = -):ARC(x,y) {
next = nxt;
}
} e[maxn];
int head[maxn],uf[maxn],tot,n,m,k;
void init() {
for(int i = ; i <= n; ++i) uf[i] = i;
}
int Find(int x) {
return uf[x] = x == uf[x]?x:Find(uf[x]);
}
void add(int u,int v,int x) {
e[tot] = ARC2(u,v,head[x]);
head[x] = tot++;
}
vector<int>MST,ans,tans;
void Kruskal() {
init();
MST.clear();
sort(g,g+k);
for(int i = ; i < k; ++i) {
int x = Find(g[i].u),y = Find(g[i].v);
if(x == y) continue;
uf[x] = y;
MST.push_back(i);
if(MST.size() >= n-) return;
}
}
void solve() {
int sum = 0x3f3f3f3f,id,tsum;
for(int i = ; i <= m; ++i) {
init();
tans.clear();
for(int j = head[i]; ~j; j = e[j].next) {
int x = Find(e[j].u),y = Find(e[j].v);
if(x != y) uf[x] = y;
}
for(int j = tsum = ; j < MST.size(); ++j) {
int x = Find(g[MST[j]].u),y = Find(g[MST[j]].v);
if(x == y) continue;
tsum += g[MST[j]].w;
uf[x] = y;
tans.push_back(MST[j]);
}
if(tsum < sum) {
sum = tsum;
id = i;
ans.clear();
std::copy(tans.begin(),tans.end(),std::back_inserter(ans));
}
}
printf("%d %d %d\n",sum,id,ans.size());
bool flag = false;
sort(ans.begin(),ans.end());
for(int i = ; i < ans.size(); ++i) {
if(flag) putchar(' ');
printf("%d",g[ans[i]].id);
flag = true;
}
putchar('\n');
}
int main() {
int a,b,c,p;
scanf("%d %d %d",&n,&m,&k);
memset(head,-,sizeof(head));
for(int i = tot = ; i < k; ++i) {
scanf("%d %d %d %d",&a,&b,&c,&p);
g[i] = ARC1(a,b,p,i + );
add(a,b,c);
}
Kruskal();
solve();
return ;
}

SGU 323 Aviamachinations的更多相关文章

  1. SGU 495. Kids and Prizes

    水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...

  2. ACM: SGU 101 Domino- 欧拉回路-并查集

    sgu 101 - Domino Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Desc ...

  3. 【SGU】495. Kids and Prizes

    http://acm.sgu.ru/problem.php?contest=0&problem=495 题意:N个箱子M个人,初始N个箱子都有一个礼物,M个人依次等概率取一个箱子,如果有礼物则 ...

  4. SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operati ...

  5. SGU 422 Fast Typing(概率DP)

    题目大意 某人在打字机上打一个字符串,给出了他打每个字符出错的概率 q[i]. 打一个字符需要单位1的时间,删除一个字符也需要单位1的时间.在任意时刻,他可以花 t 的时间检查整个打出来的字符串,并且 ...

  6. sgu 104 Little shop of flowers 解题报告及测试数据

    104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...

  7. 树形DP求树的重心 --SGU 134

    令一个点的属性值为:去除这个点以及与这个点相连的所有边后得到的连通分量的节点数的最大值. 则树的重心定义为:一个点,这个点的属性值在所有点中是最小的. SGU 134 即要找出所有的重心,并且找出重心 ...

  8. SGU 170 Particles(规律题)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=170 解题报告:输入两个由'+'和'-'组成的字符串,让你判断第二个串能不能由第一个 ...

  9. SGU 179 Brackets light(生成字典序的下一个序列)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=179 解题报告:输入一个合法的括号串,求出这个括号串的字典序的下一个串.(认为'(' ...

随机推荐

  1. IDEA无法启动:Failed to create JVM:error code -4

    发生该错误的原因是由于IDEA须要使用的连续内存空间没有得到满足,解决方式: 1.减小-Xmx和-XX:PermSize的值     切换到IDE_HOME\bin\文件夹下,找到<produc ...

  2. Git(三):加入与提交

     在这一节.接着使用上一节的代码样例往下讲,http://blog.csdn.net/troy__/article/details/39806245. 加入文件到暂存区      加入新文件和改动版本 ...

  3. Autofac依赖注入框架

    最近使用Autofac框架做项目的依赖注入,感觉挺好用的. 没有深入研究,只是拿来用用,具体可以去官网看看:https://autofac.org/. 这里只是贴一下最近项目的配置: public p ...

  4. HTML5,CSS3新特性,与旧版的区别

    HTML5新特性 (1)语意化更好的内容元素,比如 article.footer.header.nav.section (2)本地存储.sessionStorage.localStorage和inde ...

  5. animation和transition做动画的区别

    animation做动画,是不需要去触发的,可以定义一开始就执行 transition做动画,是需要人为触发,才能执行的

  6. php基础:define()定义常数函数

    define(); 常量类似变量,不同之处在于: 在设定以后,常量的值无法更改 常量名不需要开头的美元符号 ($) 作用域不影响对常量的访问 常量值只能是字符串或数字 <?php define( ...

  7. vue的鼠标移入和移出

    vue的鼠标移入和移出 需求(鼠标到预约二维码显示,预约添加背景色) 实现 <!--html部分--> <ul class="person_list"> / ...

  8. 洛谷1073 NOIP2009 最优贸易

    题目大意 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双 ...

  9. 第一章、zabbix安装

    前言: 注意:本文不涉及性能测试.性能优化中的监控,所有文字的出发点都是日常运维监控. 在开始之前,我们还是先统一下认识:要监控一个对象,需要掌握哪些东西呢? 监控对象的理解:要监控的对象你是否了解呢 ...

  10. Unity 编辑器学习(四)之 静态游戏物体

    一.Static GameObjects 关于静态对象的信息往往可以预先在编辑器里计算,不需要实时计算,进而优化性能,明显降低DrawCall. 各种Static说明: Lightmapping: 用 ...