Light OJ 1049 - One Way Roads (暴力 or DFS)
来源:互联网 发布:安卓graviboard软件 编辑:程序博客网 时间:2024/06/05 07:34
传送门:http://lightoj.com/volume_showproblem.php?problem=1049
题目大意:给定n个地点,n个点连成一个环,点与点之间是有向边,求使得图成为强连通图的最小花费。
解题思路:n范围比较小,两层FOR暴力或者DFS找出正向边和反向边即可,然后比较两个的权值和大小,即可求出答案。
Code:
/* W w w mm mm 222222222 7777777777777 *//* W w w w m m m m 222 22 7777 *//* w w w w m m m m 22 777 *//* w w w w m m m m 22 77 *//* w w w w m m m m 222 77 *//* w w w w m m m m 222 77 *//* w w w w m m m m 222 77 *//* w w w w m m m m 222 77 *//* w w w w m m m m 222 77 *//* ww ww m mm m 222222222222222 77 *///#pragma comment(linker, "/STACK:102400000,102400000")//C++//int size = 256 << 20; // 256MB//char *p = (char*)malloc(size) + size;//__asm__("movl %0, %%esp\n" :: "r"(p));//G++#include<set>#include<map>#include<queue>#include<stack>#include<ctime>#include<deque>#include<cmath>#include<vector>#include<string>#include<cctype>#include<cstdio>#include<cstdlib>#include<cstring>#include<sstream>#include<iostream>#include<algorithm>#define REP(i,s,t) for(int i=(s);i<=(t);i++)#define REP2(i,t,s) for(int i=(t);i>=s;i--)using namespace std;typedef long long ll;typedef unsigned long long ull;typedef unsigned long ul;const int N=105;int T;int n;struct Edge{ int u,v,w;}E[N];bool vis[N];int main(){ #ifdef ONLINE_JUDGE #else freopen("test.in","r",stdin); #endif int ca=1; scanf("%d",&T); while(T--) { scanf("%d",&n); int tot=0; REP(i,1,n) { scanf("%d%d%d",&E[i].u,&E[i].v,&E[i].w); tot+=E[i].w; } int res=0; memset(vis,0,sizeof(vis)); int cnt=1; int curu=E[1].u,curv=E[1].v; vis[1]=1; while(1) { if(cnt==n) { break; } REP(i,1,n) { if(!vis[i]) { if(curv==E[i].u) { vis[i]=1; curu=E[i].u; curv=E[i].v; cnt++; break; } else if(curv==E[i].v) { vis[i]=1; curu=E[i].v; curv=E[i].u; res+=E[i].w; cnt++; break; } } } } printf("Case %d: %d\n",ca++,min(res,tot-res)); } return 0;}
0 0
- Light OJ 1049 - One Way Roads (暴力 or DFS)
- LightOJ--1049--One Way Roads(dfs)(好题)
- lightoj 1049 - One Way Roads 【DFS】
- LightOJ - 1049 One Way Roads
- LightOJ 1049 One Way Roads
- lightoj1049 - One Way Roads
- Light OJ 1002 - Country Roads
- ZSC oj 1538(DFS||暴力)
- Light OJ 1188 Fast Queries(分块暴力)
- Light OJ 1339 Strongest Community(分块暴力)
- Light OJ:1307 Counting Triangles(二分+暴力枚举)
- light oj 1337--DFS--分块
- Light OJ - 1002 - Country Roads 题解
- Light oj 1002 - Country Roads 迪杰斯特拉变形
- Light oj 1012--dfs(首次觉得自己会了)
- Light oj 1060 - nth Permutation(dfs 计数)
- Light OJ 1226 One Unit Machine(DP)
- one way or two way location tdoa rtof
- rbac权限设计思路
- [leetcode] 132.Palindrome Partitioning II
- hdu4497 GCD and LCM
- JSP页面中预览本地图片
- 暑假期望和学习
- Light OJ 1049 - One Way Roads (暴力 or DFS)
- Redis_conf配置文件说明
- c++ 常量指针 const*const
- GRE写作必备句型
- ios 开发之获取手机的外网IP和内网IP
- Swift入坑--@selector
- Opencv代码小结之Mat三维创建和Mat逐点逐行赋值
- git实践
- Android EventBus源码解析 带你深入理解EventBus