hdu1862字典序

主要是字典序排序

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct student
{
    char num[7];
    char name[10];
    int score;
}stu[100002];
bool cmp1(const student &a, const student &b)
{
    return strcmp(a.num, b.num)<0?1:0;
}
bool cmp2(const student &a, const student &b)
{
    if(strcmp(a.name, b.name))
        return strcmp(a.name, b.name)<0?1:0;
    else
        return strcmp(a.num, b.num)<0?1:0;
}
bool cmp3(const student &a, const student &b)
{
    if(a.score!=b.score)
        return a.score < b.score;
    else
        return strcmp(a.num, b.num)<0?1:0;
}
int main()
{
    int n, c, k = 0;
    while(scanf("%d%d", &n, &c)!=EOF)
    {
        k++;
        if(n==0)break;
        for(int i = 1; i <= n; i++)
        {
            scanf("%s%s%d", stu[i].num, stu[i].name, &stu[i].score);
        }
        printf("Case %d:\n", k);
        if(c == 1)
            {
                sort(stu+1, stu+n+1, cmp1);
                for(int i = 1; i <= n; i++)
                    printf("%s %s %d\n", stu[i].num, stu[i].name, stu[i].score);
                    continue;
            }
        if(c == 2)
        {
            sort(stu+1, stu+n+1, cmp2);
            for(int i = 1; i <= n; i++)
                printf("%s %s %d\n", stu[i].num, stu[i].name, stu[i].score);
            continue;
        }
        if(c == 3)
        {
            sort(stu+1, stu+n+1, cmp3);
            for(int i = 1; i <= n; i++)
                printf("%s %s %d\n", stu[i].num, stu[i].name, stu[i].score);
            continue;
        }
    }
    return 0;
}

HDU 4109是一道经典的算法题目,题目名称为“Agent J”。这道题目主要考察的是图论中的最短路径算法,特别是Dijkstra算法的应用。 题目描述: 在一个有向图中,给定起点和终点,求从起点到终点的最短路径。如果存在多条最短路径,输出字典序最小的路径。 解题思路: 1. 使用Dijkstra算法计算从起点到终点的最短路径。 2. 在Dijkstra算法的基础上,使用优先队列来确保找到的路径字典序最小。 3. 使用一个数组来记录每个节点的前驱节点,以便最后可以回溯出完整的路径。 代码实现: ```java import java.util.*; public class Main { static class Edge { int to, weight; Edge(int to, int weight) { this.to = to; this.weight = weight; } } static class Node implements Comparable<Node> { int id, dist; Node(int id, int dist) { this.id = id; this.dist = dist; } @Override public int compareTo(Node other) { return this.dist - other.dist; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); while (T-- > 0) { int n = scanner.nextInt(); int m = scanner.nextInt(); List<List<Edge>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < m; i++) { int from = scanner.nextInt(); int to = scanner.nextInt(); int weight = scanner.nextInt(); graph.get(from).add(new Edge(to, weight)); } int start = scanner.nextInt(); int end = scanner.nextInt(); int[] dist = new int[n + 1]; Arrays.fill(dist, Integer.MAX_VALUE); dist[start] = 0; int[] prev = new int[n + 1]; Arrays.fill(prev, -1); PriorityQueue<Node> pq = new PriorityQueue<>(); pq.offer(new Node(start, 0)); while (!pq.isEmpty()) { Node current = pq.poll(); if (current.id == end) break; if (current.dist > dist[current.id]) continue; for (Edge edge : graph.get(current.id)) { if (dist[edge.to] > current.dist + edge.weight) { dist[edge.to] = current.dist + edge.weight; prev[edge.to] = current.id; pq.offer(new Node(edge.to, dist[edge.to])); } else if (dist[edge.to] == current.dist + edge.weight && prev[edge.to] > current.id) { prev[edge.to] = current.id; } } } if (dist[end] == Integer.MAX_VALUE) { System.out.println(-1); } else { List<Integer> path = new ArrayList<>(); int current = end; while (current != -1) { path.add(current); current = prev[current]; } Collections.reverse(path); for (int i = 0; i < path.size(); i++) { System.out.print(path.get(i) + (i < path.size() - 1 ? " " : "\n")); } } } scanner.close(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值