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. JavaSript之prototype属性

    近期在JavaSript进行Array操作的时候发现没有删除节点的方法.而我要实现的效果须要不断调用删除节点的方法.查找了相关资料发现能够利用prototype属性给Array添加删除节点的方法.而且 ...

  2. Angry IP Scanner 获取设备的IP

    给大家介绍一款软件Angry IP scanner,这款软件最大的用处就是能够扫描某一网段的各个主机的ip.通过使用发现,原理就是通过高速的ping每一个ip,假设有主机存在.就获取这个主机的user ...

  3. cocos2d-iphone 动作

    (1)CCMoveTo [CCMoveTo alloc]initWithDuration:<#(ccTime)#> position:<#(CGPoint)#> 參数说明 : ...

  4. sqoop配置安装以及导入

    安装sqoop的前提是已经具备java和hadoop的环境 1.上传并解压 (要导mysql的数据)得加入mysql的jdbc驱动包 接下来验证启动 Sqoop的数据导入 “导入工具”导入单个表从RD ...

  5. BZOJ 3277/3473 广义后缀自动机

    说实话没啥难的. 建一棵广义后缀自动机,暴力自底向上更新即可. 时间复杂度非常玄学,但据说是可以过的. 要注意每个串中相同的子串的贡献是都要加进去的,开始因为这个被坑了好久 QAQ Code: #in ...

  6. C#中Dictionary排序方式

    转载自:https://www.cnblogs.com/5696-an/p/5625142.html 自定义类: https://files.cnblogs.com/files/xunhanliu/d ...

  7. [JSOI2007]文本生成器(AC自动机+DP)

    题意 给你n个串.问有多少长度为m的串使得这n个串至少在其中出现过一次.输出答案膜10007意义下的结果. (n<=100,每个串的长度<=100) 题解 在AC自动机上跑DP. 用到一个 ...

  8. Linux中常用命令(文件)

    1.cat 显示出文件的全部内容 (1)格式:cat 文件名 -n 显示行号 (2)特点:一次性显示所有文件内容 2.tac 从最后一行倒着显示文件全部内容 3.more 全屏方式分页显示文件内容 回 ...

  9. ssh 免交互式登陆

    脚本: vim key.sh #!/bin/bash#make key\rm -f /root/.ssh/id_dsassh-keygen -t dsa -f /root/.ssh/id_dsa -P ...

  10. shiro整合thymeleaf

    1.引入依赖 <!--thymeleaf中使用shiro--> <dependency> <groupId>com.github.theborakompanioni ...