One Way Roads(搜索)

One Way Roads

Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Submit Status Practice LightOJ 1049

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). Nextn lines contain description of roads. Each road is described by three integers ai, bi, ci (1ai,bin,aibi,1ci100) - road is directed from city ai to city bi, redirecting the traffic costs ci.

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<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
#include<set>
using namespace std;
const int INF = 0x3f3f3f3f;
#define mem(x, y) memset(x, y, sizeof(x))
typedef long long LL;
void SI(int &x){scanf("%d", &x);}
void SI(LL &x){scanf("%lld", &x);}
int mp[110][110];
vector<int>vec[110];
int vis[110];
int ans;
int n;
int dot[110];
int tp;
void dfs(int u){
    for(int i = 0; i < vec[u].size(); i++){
        int v = vec[u][i];
        if(vis[v])continue;
        dot[++tp] = v;
        vis[v] = 1;
        dfs(v);
    }
}
int main(){
    int T, a, b, c, kase = 0;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        mem(mp,0);
        mem(vis,0);
        for(int i = 0; i < 110; i++)vec[i].clear();
        for(int i = 0; i < n; i++){
            scanf("%d%d%d", &a, &b, &c);
            mp[a][b] = c;
            vec[a].push_back(b);
            vec[b].push_back(a);
        }
        tp = 0;
        dot[++tp] = 1;
        vis[1] = 1;
        dfs(1);
        int ans1 = 0, ans2 = 0;
        for(int i = 2; i <= tp; i++){
            int u = dot[i - 1], v = dot[i];
            //printf("u = %d v = %d\n", u, v);
            if(mp[u][v] == 0){
                ans1 += mp[v][u];
            }
        }
        if(mp[dot[tp]][dot[1]] == 0)
            ans1 += mp[dot[1]][dot[tp]];
            //printf("ans1 = %d\n", ans1);
        for(int i = tp; i >= 2; i--){
            int u = dot[i], v = dot[i - 1];
        //    printf("u = %d v = %d\n", u, v);
            if(mp[u][v] == 0){
                ans2 += mp[v][u];
            }
        }
        if(mp[dot[1]][dot[tp]] == 0)
            ans2 += mp[dot[tp]][dot[1]];
        //printf("ans2 = %d\n", ans2);
        printf("Case %d: %d\n", ++kase, min(ans1, ans2));
    }
    return 0;
}
时间: 2024-08-28 23:49:26

One Way Roads(搜索)的相关文章

Codeforces 238E. Meeting Her 图论+记忆化搜索

大意: 有一个 n 个结点的有向图,边权均为 1.Urapl 想从 a 出发去 b.有 p 个公交车公司.在每 一秒的开始,第 i 个公司的公交车随机选择一条从 s i 到 t i 的最短路径然后走这条路径.如果 一个公交车经过 Urpal 所在的交叉点,则 Urpal 可以上这辆公交车,他可以在中途任意一个结 点下车. 在任何时刻 Urpal 只知道他自己的位置和约会地点.当他上了公交车时他只知道这辆公交 车属于第几个公司.当然 Urpal 知道城市地图和每个公司的 (s i , t i ).

HDUJ 1242 Rescue 搜索

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15582    Accepted Submission(s): 5656 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is

lightoj 1049 - One Way Roads(dfs)

Time Limit: 0.5 second(s) Memory Limit: 32 MB 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 

ACM: 强化训练-Roads in the North-BFS-树的直径裸题

Roads in the North Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that the

多次访问节点的DFS POJ 3411 Paid Roads

POJ 3411 Paid Roads Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6553   Accepted: 2430 Description A network of m roads connects N cities (numbered from 1 to N). There may be more than one road connecting one city with another. Some o

POJ 3249 Test for Job (记忆化搜索 好题)

Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9512   Accepted: 2178 Description Mr.Dog was fired by his company. In order to support his family, he must find a new job as soon as possible. Nowadays, It's hard to have a job

sicily 1024 邻接矩阵与深度优先搜索解题

Description There are N cities and N-1 roads in Magic-Island. You can go from one city to any other. One road only connects two cities. One day, The king of magic-island want to visit the island from the capital. No road is visited twice. Do you know

每日一水之 luogu2907 [USACO08OPEN]农场周围的道路Roads Around The Farm

题目描述 Farmer John's cows have taken an interest in exploring the territory around the farm. Initially, all N (1 <= N <= 1,000,000,000) cows commence traveling down a road in one big group. Upon encountering a fork in the road, the group sometimes cho

POJ3249 Test for Job 【DAG】+【记忆化搜索】

Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9201   Accepted: 2080 Description Mr.Dog was fired by his company. In order to support his family, he must find a new job as soon as possible. Nowadays, It's hard to have a job