A. Ring road

针对一国城市间的环形交通网络,在引入单向交通规则后出现可达性问题,本研究探讨了如何通过调整最少数量的道路方向来确保任意两城市间的连通性,并计算所需的最小成本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Ring road
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Berland decided to keep up with new trends. Formerly all n cities of Berland 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 Berland introduced one-way traffic on all n roads, 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

The first line contains integer n (3?≤?n?≤?100) — amount of cities (and roads) in Berland. Next n lines contain description of roads. Each road is described by three integers aibici (1?≤?ai,?bi?≤?n,?ai?≠?bi,?1?≤?ci?≤?100) — road is directed from city ai to city bi, redirecting the traffic costs ci.

Output

Output single integer — 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 test(s)
input
3
1 3 1
1 2 1
3 2 1
output
1
input
3
1 3 1
1 2 5
3 2 1
output
2
input
6
1 5 4
5 3 8
2 4 15
1 6 16
2 3 23
4 6 42
output
39
input
4
1 2 9
2 3 8
3 4 7
4 1 5
output
0
很好的题目
/* ***********************************************
Author :
Created Time :2015/6/11 21:50:17
File Name :7.cpp
************************************************ */

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 1<<30
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
return a>b;
}
int a[110][110];
vector<int>v[maxn];
void init(){
for(int i=1;i<=102;i++)
for(int j=1;j<=102;j++)
a[i][j]=INF;
}
int n;
int ans;
int bs(int t){
//cout<<1<<" "<<t<<endl;
ans=0;
int cnt=0;
int p=1;
if(a[1][t]==INF)ans+=a[t][1];
while(1){
cnt++;
for(int i=0;i<=1;i++){
int j=v[t][i];
if(j!=p){
// cout<<t<<" "<<j<<endl;
if(a[t][j]==INF)ans+=a[j][t];
p=t;
t=j;
}
if(t==1)break;
}
if(t==1)break;
if(cnt==n-1)break;

}
return ans;
}
/*int dfs(int t,int p){
int j;
if(t==1){
if(a[t][p]==INF)ans+=a[p][t];
return ans;
}
else for(int i=0;i<1;i++){
int j=v[t][i];
if(j!=p){
if(a[t][j]==INF)ans+=a[j][t];
dfs(j,t);
}
}
//return ans;
}*/
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
while(cin>>n){
init();
int x,y,z;
for(int i=1;i<=n;i++){
a[i][i]=0;
cin>>x>>y>>z;
a[x][y]=z;
v[x].push_back(y);
v[y].push_back(x);
}
//ans=0;
//cout<<bs(v[1][0]);//<<" "<<bs(v[1][1])<<endl;
//cout<<dfs(v[1][0],1)<<endl;
cout<<min(bs(v[1][0]),bs(v[1][1]))<<endl;
}
return 0;
}


/*********************************************************************************************************************4月17号之前的,最近正在修改的代码 * @file main.c * @brief 智能车空工程 * @author * @Target core GD32F303RCT6 * @revisions 2022.10.08, V1.0 * @modify none ********************************************************************************************************************/ #include "main.h" #include "headfile.h" #include "gd32f30x.h" #define a_error 1 #define b_error 1 #define c_error 1 #define base_speed 3000 uint16_t max_adc[5] ={3500,3600,3500,3600,3500}; //电感最大值 uint16_t min_adc[5]={0,0,0,0,0}; //电感最小值 uint16_t max[5] = {0,0,0,0,0}; //滤波用最大值 uint16_t min[5] = {3500,3500,3500,3500,3500}; //滤波用最小值 uint16_t ad_value[5]={0}; //AD采集值 uint32_t ad_filter_value[5] = {0};//滤波后的平均值 uint16_t ad_m100_value[5]={0}; //归100的值 double dif_rat_04 = 0; //0和4号电感的差比和 float dif_rat_40 = 0; //4和0号电感的差比和 float dif_rat_13 = 0; //1和3号电感的差比和 uint32_t rat_04 =0; uint32_t rat_40 =0; double error1,error2,error3,error4,error_all; uint16_t flag_road = 0; uint16_t right_ring =0; uint16_t left_ring = 0; void sensor_calibration() { for(int i=0;i<500;i++){ for(uint8_t ch=0;ch<5;ch++) { uint16_t val=adc_get(ADC0,ADC_CH_10+ch); if(val > max_adc[ch])max_adc[ch]=val; if(val < min_adc[ch])min_adc[ch]=val; } delay_1ms(10); } max_adc[1]+=100; max_adc[3]+=100; for(uint8_t ch=0;ch<5;ch++) { if(max_adc[ch]-min_adc[ch]<300) { max_adc[ch]=min_adc[ch]+300; } } } int main(void) { /* 此处声明需要用到的局部变量 */ unsigned int adc_value[5]; car_init(); //智能车初始化 oled_init(); //OLED初始化 /* 此处编写单次运行的代码(例如:初始化代码等) */ oled_show_string(0,0,(uint8_t*)"L0",12);// oled_show_string(25,0,(uint8_t*)"L1",12);// oled_show_string(50,0,(uint8_t*)"L2",12);// oled_show_string(75,0,(uint8_t*)"L3",12);// oled_show_string(100,0,(uint8_t*)"L5",12);// while(1) { board_led_toggle(LED1); //指示灯,观察程序运行 for (uint8_t x = 0; x < 5; x++) { ad_filter_value[x] = 0; //对各个电感平均值清零。 ad_m100_value[x] = 0; max[x]=0; min[x]=3300; } for(uint8_t k = 0;k < 7; k++) // 每个电感连续采集7次,为了提高数据稳定 { adc_value[0] = adc_get(ADC0, ADC_CH_10); adc_value[1] = adc_get(ADC0, ADC_CH_11); adc_value[2] = adc_get(ADC0, ADC_CH_12); adc_value[3] = adc_get(ADC0, ADC_CH_13); adc_value[4] = adc_get(ADC0, ADC_CH_15); delay_1ms(2); for(uint8_t x = 0; x < 5; x++) //每次采集后,对5个电感采集值进行叠加求和 { if(adc_value[x] >= max[x]) max[x] = adc_value[x] ; if (adc_value[x] <= min[x]) min[x] = adc_value[x] ; ad_filter_value[x] = ad_filter_value[x]+adc_value[x]; //得到各个电感的7次数据之和 } } for (uint8_t x = 0; x < 5; x++) { ad_filter_value[x] = ad_filter_value[x]-max[x]-min[x]; //对各个电感的7次和去掉最大和最小值。 ad_filter_value[x] = (uint32_t)(ad_filter_value[x] / 5); //还剩下5个,求平均值 //对电感值进行归100化;让(电感实际值 - 电感最小值)/(电感最大值 - 电感最小值)*100 ad_m100_value[x] =(uint16_t) (ad_filter_value[x]-min_adc[x])*100/(max_adc[x]-min_adc[x]); } //两电感融合的差比和值,能够有效的让电感车在赛道平滑运行。 // dif_rat_04= ((double)ad_filter_value[0]-(double)ad_filter_value[4])*100/((double)ad_filter_value[0] + (double)ad_filter_value[4]); // dif_rat_13 = ((double)ad_filter_value[1]-(double)ad_filter_value[3])*100/((double)ad_filter_value[1] + (double)ad_filter_value[3]); error1 = a_error*((double)ad_filter_value[0]-(double)ad_filter_value[4]); error2 = b_error*((double)ad_filter_value[1]-(double)ad_filter_value[3]); error3 = a_error*((double)ad_filter_value[0]+(double)ad_filter_value[4]); error4 = c_error*fabs((double)ad_filter_value[1]-(double)ad_filter_value[3]) ; error_all = (double)((error1 +error2)/(error3+error4)*100); //归100 if(error_all>=0) { oled_show_char(0,6,'+',12); oled_show_number(0+10,6,error_all,4,12); //查看差比和的值 } else { oled_show_char(0,6,'-',12); oled_show_number(0+10,6,-error_all,4,12); //查看差比和的值 } if((ad_m100_value[1]>50)&(ad_m100_value[2]>50)) //左直角 { flag_road = 10; motor_forward(left, base_speed+300); motor_forward(right, base_speed); //用差比和的值调整电机转速。其中 50是根据实际情况进行调整的。 } else if((ad_m100_value[2]>50)&(ad_m100_value[3]>50)) //右直角 { flag_road = 01; motor_forward(left, base_speed); motor_forward(right, base_speed+300); //用差比和的值调整电机转速。其中 50是根据实际情况进行调整的。 } else if((ad_m100_value[2]>60)&(ad_m100_value[4]>60))//进右环 { flag_road = 12; right_ring++; if (right_ring==1) { delay_1ms(300); } if(right_ring ==2) { motor_forward(left, base_speed); motor_forward(right, base_speed+500); //用差比和的值调整电机转速。其中 50是根据实际情况进行调整的。 delay_1ms(300); } if(right_ring ==3) right_ring=0; } else if((ad_m100_value[2]>60)&(ad_m100_value[0]>60))//进左环 { flag_road = 21; left_ring++; if (left_ring==1) { delay_1ms(300); } if(left_ring ==2) { motor_forward(left, base_speed+500); motor_forward(right, base_speed); //用差比和的值调整电机转速。其中 50是根据实际情况进行调整的。 delay_1ms(300); } if(left_ring ==3) left_ring=0; } else { motor_forward(left, base_speed+error_all*10); motor_forward(right, base_speed-error_all*10); //用差比和的值调整电机转速。其中 50是根据实际情况进行调整的。 } for(uint8_t i=0;i<5;i++) //进行集中显示 { oled_show_number(0+26*i,1,adc_value[i],4,12); //显示电感最大值 oled_show_number(0+26*i,2,min_adc[i],4,12); //显示电感最小值 oled_show_number(0+26*i,3,ad_filter_value[i],4,12); //显示电感平均值 oled_show_number(0+26*i,4,ad_m100_value[i],4,12); //显示电感归100后的值 } } } 给智能车加权
最新发布
07-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值