CodeForces CF 360E Levko and Game 贪心+SPFA

本文介绍了一个比赛场景下的路径寻找问题,玩家需要通过调整部分可变边的权重来确保从起点到达终点的时间优于对手。通过使用SPFA算法进行多次迭代,找到能够确保获胜或至少打平的边权重设置。

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

听说是一道贪心题。。

已知N个点,M+K条有向边及边权,可能有重边和自环。

可以消耗费用修改给定k条边中一些边的边权。

第i条边边权修改范围[li,ri]。

修改给定K条边的长度使从s1到f比s2到f的时间短。


对于边<x,y>,如果dis(s1,x)<dis(s2,x),即如果2条最短路通过x,那么dis(s1,f)必小于dis(s2,f),那么<x,y>的边长修改为l可以使dis(s1,f)更小。

如此修改+重跑最短路后若dis(s1,f)<dis(s2,f)则可以输出WIN,=输出DRAW。

对于平局,判断dis(s1,x)<=dis(s2,x)即可。


发现一定要写if(!check(0))if(!check(1))并不能&&....说好的不满足条件不继续执行的优化呢。。。

#include <cstdio>
#include <cstring>
#define FOR(i,j,k) for(i=j;i<=k;i++)
int read() {
    int s=0,f=1;char ch=getchar();
    for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
    for(;'0'<=ch&&ch<='9';ch=getchar())s=s*10+ch-'0';
    return s*f;
}
typedef long long ll;
const int N = 10001, M = 20001;

ll da[N], db[N], el[M], er[M];
int n, m, k, s1, s2, t;
int head[N], next[M], from[M], to[M], q[N * 64], cnt = 0;
bool vis[N];

void add(int u, int v, int l, int r) {
    next[++cnt] = head[u]; from[cnt] = u; to[cnt] = v;
    el[cnt] = l; er[cnt] = r; head[u] = cnt;
}

void spfa(ll dis[N], int s) {
    memset(dis, 0x7f, sizeof(ll)*N);
    memset(vis, 0, sizeof vis);
    int f = 0, r = 0, i, u;
    q[r++] = s; dis[s] = 0; vis[s] = 1;
    while (f < r) {
        u = q[f++];
        for (i = head[u]; i; i = next[i]) {
            if (dis[to[i]] > dis[u] + er[i]) {
                dis[to[i]] = dis[u] + er[i];
                if (!vis[to[i]]) {
                    q[r++] = to[i];
                    vis[to[i]] = 1;
                }
            }
        }
        vis[u] = 0;
    }
}

bool check(int f) {
    int flag = 1, i;
    while (flag) {
        flag = 0;
        spfa(da, s1); spfa(db, s2);
        FOR(i,m+1,m+k)
            if(da[from[i]]<db[from[i]] + f && el[i] != er[i])
                er[i] = el[i], flag = 1;
        if (da[t] < db[t] + f) {
            puts(f ? "DRAW" : "WIN");
            FOR(i,m+1,m+k) printf("%d ", er[i]);
            return 1;
        }
    }
    return 0;
}

int main() {
    n = read(), m = read(), k = read();
    s1 = read(), s2 = read(), t = read();
    int i, x, y, z, l, r;
    FOR(i,1,m) x=read(),y=read(),z=read(),add(x,y,z,z);
    FOR(i,1,k) x=read(),y=read(),l=read(),r=read(),add(x,y,l,r);
    
    if(!check(0)) if(!check(1)) puts("LOSE");
    return 0;
}


Levko loves sports pathfinding competitions in his city very much. In order to boost his performance, Levko spends his spare time practicing. The practice is a game.

The city consists of n intersections connected by m + k directed roads. Two or more roads can connect the same pair of intersections. Besides, there can be roads leading from an intersection to itself.

Levko and Zenyk are playing a game. First Levko stands on intersection s1, and Zenyk stands on intersection s2. They both want to get to intersection f. The person who does it quicker wins. If they get there at the same time, the game ends with a draw. By agreement both players start simultaneously and move with the same speed.

Levko wants to win very much. He knows the lengths of all the roads in the city. Also he knows that he can change the lengths of some roads (there are k such roads at all) if he pays the government. So, the government can change the length of the i-th road to any integer value in the segment [liri] (both borders inclusive). Levko wondered if he can reconstruct the roads so as to win the game and whether he can hope for the draw if he cannot win.

You should consider that both players play optimally well. It is guaranteed that we can get from intersections s1 and s2 to intersection f.

Input

The first line contains three integers nm and k (1 ≤ n, m ≤ 1041 ≤ k ≤ 100). The second line contains three integers s1s2 and f(1 ≤ s1, s2, f ≤ n).

The next m lines contains the descriptions of the roads that cannot be changed by Levko. Each line contains three integers aibi and ci(1 ≤ ai, bi ≤ n, 1 ≤ ci ≤ 109), representing a road from intersection ai to intersection bi of length ci.

The next k lines contains the descriptions of the roads that can be changed by Levko. Each line contains four integers aibili and ri(1 ≤ ai, bi ≤ n, 1 ≤ li ≤ ri ≤ 109), representing a road from intersection ai to intersection bi, Levko can set the road's length within limits[li, ri].

Consider all intersections numbered from 1 to n. It is guaranteed that you can get from intersections s1 and s2 to intersection f.

Output

In the first line print string "WIN" (without the quotes) if Levko can win this game, string "DRAW" (without the quotes) if Levko can end the game with a draw and "LOSE" (without the quotes) if he loses for sure.

If the answer is "WIN" or "DRAW", then print on the second line k space-separated integers — the length of the roads Levko sets in the order they occur in the input.

Sample test(s)
input
4 1 3
1 3 4
3 2 2
1 2 1 3
2 4 1 3
3 4 1 3
output
WIN
1 1 3 
input
4 1 3
1 3 4
3 2 2
1 2 1 3
2 4 1 3
3 4 1 2
output
DRAW
1 1 2 
input
5 4 2
1 2 5
1 3 3
1 4 4
2 3 2
2 4 3
3 5 1 5
4 5 4 7
output
LOSE
### CodeForces平台上关于SPFA算法的问题和资源 CodeForces作为一个知名的在线编程竞赛平台,提供了丰富的题目库供用户练习不同类型的算法问题。对于寻找特定于SPFA(Shortest Path Faster Algorithm)算法的问题,在该网站上可以通过标签功能来定位相关题目[^1]。 然而需要注意的是,由于SPFA并非像Dijkstra那样被广泛设为独立分类标签,因此可能不会直接找到标记有此名称的题目。建议通过搜索含有图论、短路径等相关关键词的挑战来进行间接筛选。 另外一种方法是在高级搜索选项中利用自定义过滤器设置难度范围以及涉及的知识点领域,从而提高发现适合练习SPFA技能题目的几率。例如可以尝试访问带有`shortest paths` 或者 `graphs` 标签并且难度等级处于中级到高级之间的题目列表。 ```python import requests from bs4 import BeautifulSoup def find_spfa_problems(): url = "https://codeforces.com/problemset" params = { 'tags': 'data structures, graphs', # Tags that might contain SPFA related problems 'order': 'BY_RATING_ASC' # Order by difficulty ascendingly } response = requests.get(url, params=params) soup = BeautifulSoup(response.text, 'html.parser') problem_list = [] for row in soup.select('table.problems tr'): columns = row.find_all('td') if len(columns) >= 2: name_cell = columns[0].find('a') rating_cell = columns[-1] if name_cell and rating_cell: title = name_cell.get_text(strip=True) link = f"https://codeforces.com{name_cell['href']}" try: rating = int(rating_cell.get_text(strip=True)) problem_list.append((title, link, rating)) except ValueError: continue filtered_problems = [(t,l,r) for t,l,r in problem_list if r>=1700 and r<=1900] return filtered_problems[:5] # Return top five matches as an example print(find_spfa_problems()) ``` 上述Python脚本展示了如何自动化地从CodeForces获取潜在适用于练习SPFA算法的数据结构与图形类别的难题链接。请注意这只是一个示范性的实现方式,并不是所有返回的结果都一定涉及到SPFA;实际应用时还需要人工进一步甄别确认哪些具体问题是基于这一算法设计的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值