XTU2016CCPC中南邀请赛C

本文介绍了一种解决Hamiltonian Path问题的方法,通过读取输入数据并寻找最优路径以实现总距离最小化。针对特定条件,文章提供了一段C++代码示例,并详细解释了其工作原理。

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

Hamiltonian Path

Accepted : 74 Submit : 189
Time Limit : 2000 MS Memory Limit : 65536 KB 

Hamiltonian Path

In ICPCCamp, there are n cities and m directed roads between cities. The i-th road going from the ai-th city to the bi-th city is ci kilometers long. For each pair of cities (u,v), there can be more than one roads from u to v.

Bobo wants to make big news by solving the famous Hamiltonian Path problem. That is, he would like to visit all the n cities one by one so that the total distance travelled is minimized.

Formally, Bobo likes to find n distinct integers p1,p2,,pn to minimize w(p1,p2)+w(p2,p3)++w(pn1,pn) where w(x,y) is the length of road from the x-th city to the y-th city.

Input

The input contains at most 30 sets. For each set:

The first line contains 2 integers n,m (2n105,0m105).

The i-th of the following m lines contains 3 integers ai,bi,ci (1ai<bin,1ci104).

Output

For each set, an integer denotes the minimum total distance. If there exists no plan, output -1 instead.

Sample Input

3 3
1 2 1
1 3 1
2 3 1
3 2
1 2 1
1 3 2

Sample Output

2
-1

注意条件ai是小于bi!这意味着什么?意味着只能1到2,2到3的来走,任何一个跳着城市编号的道路都是无用条件,只需要把最小的相邻城市的路加起来就行了。。。mp[i]表示i到i+1的城市路径,只要条件中缺少一个mp[i]就不能满足,只能输出-1

#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
const int maxn = 1e4;
int mp[maxn+10];
ll ans;

int main() {
//    freopen("in.txt","r",stdin);
    int n,m;
    int flag = 0;
    while(~scanf("%d%d",&n,&m)) {
        ans = 0LL;
        for(int i = 1; i <= maxn; i++) mp[i] = maxn;
//        cout<<ans;
        while(m--) {
            int a,b,c;
            cin>>a>>b>>c;
            if(a==b-1) {
                if(mp[a]>c) {
                    mp[a] = c;
                }
            }
        }
        for(int i = 1; i < n; i++) {
            if(mp[i]!=maxn) {
                ans += mp[i];
            }
            else {
                puts("-1");
                flag = 1;
                break;
            }
        }
        if(!flag)
            cout<<ans<<endl;
    }
}


转载于:https://www.cnblogs.com/zhangmingzhao/p/7256619.html

### XTU OJ 密码实现中的C语言指针示例 在C语言中,指针是一种非常强大的工具,可以用来操作内存地址并间接访问数据。对于XTU OJ密码相关的功能实现,假设我们需要通过指针来处理字符串加密或解密逻辑,则可以通过以下方式完成。 #### 使用指针对字符串进行简单加密 下面是一个简单的例子,展示如何利用指针遍历字符串并对字符逐一进行位移加密: ```c #include <stdio.h> #include <string.h> void encrypt(char *str, int shift) { char *ptr = str; // 定义指向字符串首地址的指针 while (*ptr != '\0') { // 遍历直到遇到字符串结束符'\0' if ((*ptr >= 'a' && *ptr <= 'z') || (*ptr >= 'A' && *ptr <= 'Z')) { *ptr = *ptr + shift; if (*ptr > 'z' && *ptr >= 'a') { // 处理小写字母溢出 *ptr -= 26; } else if (*ptr > 'Z' && *ptr >= 'A') { // 处理大写字母溢出 *ptr -= 26; } } ptr++; // 移动到下一个字符位置 } } int main() { char password[] = "SecurePassword"; // 原始密码 printf("Original Password: %s\n", password); encrypt(password, 3); // 对密码进行加密,偏移量为3 printf("Encrypted Password: %s\n", password); return 0; } ``` 上述代码展示了如何定义一个函数 `encrypt` 来对输入字符串执行凯撒加密算法[^1]。该函数接受两个参数:一个是待加密的字符串指针,另一个是指定字母移动的距离(即偏移量)。程序会逐一遍历字符串中的每一个字符,并将其按照指定规则进行修改。 #### 关键点解析 - **指针初始化**:`char *ptr = str;` 将传入的字符串起始地址赋给局部变量 `ptr`。 - **循环条件判断**:`while (*ptr != '\0')` 判断当前指针所指向的内容是否为字符串终止标志 `\0`。 - **字符范围检测与调整**:当字符超出英文字母界限时重新计算其有效值[^2]。 - **指针自增**:每次迭代后更新指针至下一字符的位置以便继续处理剩余部分。 此方法不仅适用于基本文本变换场景,在实际应用中也可以扩展成更复杂的编码机制以满足不同需求下的安全性考量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值