T1001 Battle Over Cities - Hard Version (35分)

#include
#include
#include
#include
using namespace std;
const int inf = (1<<31)-10;
const int maxv = 510, maxe = 2e5;
int N, M;
int cost[maxv], fa[maxv];
struct Edge{
int x, y, c, s;
void read(){
scanf("%d%d%d%d", &x, &y, &c, &s);
}
bool operator<(const Edge e)const{
return s == e.s ? c < e.c : s > e.s;
}
}edge[maxe];
int get(int x){
return x == fa[x] ? x : fa[x] = get(fa[x]);
}
int main(){
scanf("%d%d", &N, &M);
for(int i = 0; i < M; i++) edge[i].read();
sort(edge, edge + M);
int ans = 0;
for(int i = 1; i <= N; i++){
cost[i] = 0;
int cnt = N-2;
for(int j = 1; j <= N; j++) fa[j] = j;
for(int j = 0; j < M; j++){
if(edge[j].x == i || edge[j].y == i) continue;
int fx = get(edge[j].x), fy = get(edge[j].y);
if(fx == fy) continue;
cnt–;
fa[fy] = fx;
if(edge[j].s == 0) cost[i] += edge[j].c;
}
if(cnt > 0) cost[i] = inf;
ans = max(ans, cost[i]);
}
if(ans > 0){
int flag = 0;
for(int i = 1; i <= N; i++){
if(ans == cost[i]){
printf("%s%d", flag==1?" “:”", i); flag = 1;
}
}
}else printf(“0”);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值