LightOJ - 1049 One Way Roads
来源:互联网 发布:dk系列图书 知乎 编辑:程序博客网 时间:2024/05/16 08:53
Description
Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Dhaka Division decided to keep up with new trends. Formerly all n cities of Dhaka were connected by n two-way roads in the ring, i.e. each city was connected directly to exactly two other cities, and from each city it was possible to get to any other city. Government of Dhaka introduced one-way traffic on all nroads, but it soon became clear that it's impossible to get from some of the cities to some others. Now for each road is known in which direction the traffic is directed at it, and the cost of redirecting the traffic. What is the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other?
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case starts with a blank line and an integer n (3 ≤ n ≤ 100) denoting the number of cities (and roads). Next n lines contain description of roads. Each road is described by three integers ai, bi, ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 100) - road is directed from city ai to city bi, redirecting the traffic costsci.
Output
For each case of input you have to print the case number and the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other.
Sample Input
4
3
1 3 1
1 2 1
3 2 1
3
1 3 1
1 2 5
3 2 1
6
1 5 4
5 3 8
2 4 15
1 6 16
2 3 23
4 6 42
4
1 2 9
2 3 8
3 4 7
4 1 5
Sample Output
Case 1: 1
Case 2: 2
Case 3: 39
Case 4: 0
题解:就是形成一个环,把它想成一个无向环,后找一个方向一直搜索,找相反的权值和,与总权值相减比较。
#include <string.h>#include <stdio.h>int pic[101][101];int mark[101];int n;int flag;int dfs(int pos,int pre){ mark[pos] = 1; if(flag==1 && pos==1)return 0; for(int i = 1; i <= n; i++) if( (pos!=1&&pre!=i&&pic[pos][i]!=0&&i==1)|| (!mark[i]&&pic[pos][i]!=0&&pre!=i)){ flag = 1; return dfs(i,pos); } for(int i = 1; i <= n; i++) if( (pos!=1&&pre!=i&&pic[i][pos]!=0&&i==1)|| (!mark[i]&&pic[i][pos]!=0&&pre!=i)){ flag = 1; return pic[i][pos]+dfs(i,pos); }}int main(){ int T; int sum; scanf("%d",&T); for(int i = 1;i <=T;i++){ scanf("%d",&n); sum = 0; memset(pic,0,sizeof(pic)); memset(mark,0,sizeof(mark)); for(int j = 0 ; j < n; j++){ int t1,t2,t3; scanf("%d%d%d",&t1,&t2,&t3); pic[t1][t2] = t3; sum += t3; } flag = 0; int ans = dfs(1,0); if(ans>sum-ans)ans = sum-ans; printf("Case %d: %d\n",i,ans); }}
- LightOJ - 1049 One Way Roads
- LightOJ 1049 One Way Roads
- lightoj 1049 - One Way Roads 【DFS】
- LightOJ--1049--One Way Roads(dfs)(好题)
- lightoj1049 - One Way Roads
- Light OJ 1049 - One Way Roads (暴力 or DFS)
- ZSTU 4213: One-Way Roads【图论】【欧拉回路】
- lightOJ 1002:Country Roads
- LightOj 1002 - Country Roads
- 【LightOJ 1002】 Country Roads
- LightOJ 1002 Country Roads
- 1002 - Country Roads(lightoj)
- One-way Web Hacking
- one way单程杀机
- One-way ANOVA
- Blizzard One-Way Hash
- React one-way flow
- codeforce723E One-Way Reform
- 利用线程和异步线程AsyncTask获取网络图片
- Microblaze的软硬件仿真
- Largest Number
- CentOS 7 编译安装Redis
- xxx is not in the sudoers file.This incident will be reported.的解决方法
- LightOJ - 1049 One Way Roads
- #ifdef __cplusplus extern "C" { #endif”的定义
- Cocos2d-x技能冷却还要等多久?---之游戏开发《赵云要格斗》(9)
- 注册
- 有人能解释android中px,dip,dp和sp这些单位的不同么?
- OTG中的ID脚风波释疑 .
- 不给发货发货人
- ARC 快速枚举Enumeration 属性错误
- Ubuntu防火墙设置