It is vitally important to have all the cities connected by highways in a war. If a city is conquered by the enemy, all the highways from/toward that city will be closed. To keep the rest of the cities connected, we must repair some highways with the minimum cost. On the other hand, if losing a city will cost us too much to rebuild the connection, we must pay more attention to that city.
Given the map of cities which have all the destroyed and remaining highways marked, you are supposed to point out the city to which we must pay the most attention.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 2 numbers N
(≤500), and M
, which are the total number of cities, and the number of highways, respectively. Then M
lines follow, each describes a highway by 4 integers: City1 City2 Cost Status
where City1
and City2
are the numbers of the cities the highway connects (the cities are numbered from 1 to N
), Cost
is the effort taken to repair that highway if necessary, and Status
is either 0, meaning that highway is destroyed, or 1, meaning that highway is in use.
Note: It is guaranteed that the whole country was connected before the war.
Output Specification:
For each test case, just print in a line the city we must protest the most, that is, it will take us the maximum effort to rebuild the connection if that city is conquered by the enemy.
In case there is more than one city to be printed, output them in increasing order of the city numbers, separated by one space, but no extra space at the end of the line. In case there is no need to repair any highway at all, simply output 0.
Sample Input 1:
4 5 1 2 1 1 1 3 1 1 2 3 1 0 2 4 1 1 3 4 1 0
Sample Output 1:
1 2
Sample Input 2:
4 5 1 2 1 1 1 3 1 1 2 3 1 0 2 4 1 1 3 4 2 1
Sample Output 2:
0
给出一个连通图,其中有些边是正常的status为1,有一些是损坏的status为0,如果要用到损坏的边那就要花费代价,正常的边不用花费,如果一个城市沦陷,问其余城市要保持连通的花费,哪个城市或者哪几个城市沦陷花费最大。如此先去掉沦陷的城市,然后用正常的边看看其他城市是否连通,连通则以,如果不连通那么就要用损坏的边来弥补,当然是先用花费小的,所以要先排序,正常边在前,损坏的在后,各自按花费再排好序。如果加上损坏的边都不能连通,这种情况是需要更多花费的,不要遗漏。
类似最小生成树的kruskal算法。
代码:
#include <cstdio> #include <vector> #include <queue> #include <algorithm> #define inf 0x3f3f3f3f using namespace std; int n,m; struct edge { int x,y,c,s; }e[250000]; int f[550]; bool cmp(edge &a,edge &b) { if(a.s != b.s) return a.s > b.s; return a.c < b.c; } inline void init() { for(int i = 1;i <= n;i ++) { f[i] = i; } } inline int getf(int k) { if(f[k] == k) return f[k]; return f[k] = getf(f[k]); } int main() { int ma = 1,ans[550],cnt = 0; scanf("%d%d",&n,&m); for(int i = 0;i < m;i ++) { scanf("%d%d%d%d",&e[i].x,&e[i].y,&e[i].c,&e[i].s); } sort(e,e + m,cmp); for(int i = 1;i <= n;i ++) { init(); int c = 0,num = 0; for(int j = 0;j < m;j ++) { if(num == n - 2) break; if(e[j].x == i || e[j].y == i) continue; int a = getf(e[j].x),b = getf(e[j].y); if(a == b) continue; f[a] = b; num ++; if(!e[j].s) c += e[j].c; } if(num != n - 2) c = inf; if(c > ma) ma = c,ans[0] = i,cnt = 1; else if(c == ma) ans[cnt ++] = i; } for(int i = 0;i < cnt;i ++) { if(i) putchar(' '); printf("%d",ans[i]); } if(!cnt) printf("0"); }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· 程序员常用高效实用工具推荐,办公效率提升利器!
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 【译】WinForms:分析一下(我用 Visual Basic 写的)
2018-02-06 剪邮票
2018-02-06 兔子繁衍问题