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. iOS_第3方类库_側滑选项卡SlideSwitchView

    终于效果: 用法: 1.在主控制器中创建一个[SlideSwitchView]的对象实例,并用成员变量记住,如_slideSwitchView,并加入到self.view 2.设置[_slideSwi ...

  2. zzulioj--1778-- 和尚特烦恼4——有多少战斗力(gcd)

    1778: 和尚特烦恼4--有多少战斗力 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 259  Solved: 123 SubmitStatusWe ...

  3. vijos--P1211--生日日数(纯模拟)

    P1211生日日数 未递交 标签:[显示标签] 描述 CCC老师的生日是YY年MM月DD日,他想知道自己出生后第一万天纪念日的日期(出生日算第0天). 格式 输入格式 从文件的第一行分别读入YY,MM ...

  4. Android框架-Volley(二)

    1. ImageRequest的用法 前面我们已经学习过了StringRequest和JsonRequest的用法,并且总结出了它们的用法都是非常类似的,基本就是进行以下三步操作即可: 1. 创建一个 ...

  5. OpenGL编程(一)渲染一个指定颜色的背景窗口

    上次已经搭好了OpenGL编程的环境.已经成功运行了第一个程序.可只是照搬书上的代码,并没弄懂其中的原理.这次通过一个小程序来解释使用GLUT库编写OpenGL程序的过程. 程序的入口 与其他程序一样 ...

  6. vue中使用console.log无效

    webpack开发环境下,在vue中使用console.log无效,一直以为webpack出了问题. 使用window.console.log()就能够顺利在浏览器控制台输出了. 以及 在axios请 ...

  7. Chrome发布73 beta版:增强Linux用户体验

    Google开发者周五推动Chrome 73进入他们的测试频道,因为他们准备在3月12日左右推出这款网页浏览器更新,以便稳定推出.除非另有说明,否则下面描述的更改适用于Android,Chrome O ...

  8. 对比学习sass和stylus的常用功能

    在众多的css预处理器语言中,sass和stylus算是十分优秀的两个.本文主要针对两者的常用功能做个简单的对比分析.在对比中了解二者的差异,同时帮助大家更好的掌握这两种预处理语言.本文涉及到的sas ...

  9. 紫书 例题 10-15 UVa 1638(递推)

    从大到小安排杆子 分三种情况 (1)插到最左边,那么左边看到了杆子会多一个 (2)插到最右边,那么右边看到了杆子会多一个 (3)插到中间边,那么不影响左边和右边看到的杆子数 具体看代码 #includ ...

  10. Java 异常的捕获与处理详解 (一)

    一,异常的产生(Exception) 异常是程序之中导致程序中断的一种指令流,异常一旦出现并且没有进行合理处理的话,那么程序就会中断执行. An exception is a flow of inst ...