今日算法
- 题目:甘老板发红包-1
超时代码:
#include <bits/stdc++.h>
using namespace std;
int n, m, u, v, cnt, head[10007], money[10007], indgr[10007];
struct Edge{
int to, next;
} edge[10007];
typedef struct Edge Edge;
void add_edge(int from, int to) {
edge[++cnt].to = to;
edge[cnt].next = head[from];
head[from] = cnt;
money[to] = max(money[to], money[from] + 1);
}
bool topsort() {
for (int i = 1; i <= n; ++i)
{
bool flag = true;
for (int j = 1; j <= n; ++j)
if (!indgr[j]) {
flag = false;
indgr[j]--;
for (int k = head[j]; ~k; k = edge[k].next)
indgr[edge[k].to]--;
break;
}
if (flag)
return true;
}
return false;
}
int main (void) {
while(~scanf("%d%d", &n, &m)) {
for (int i = 1; i <= n; ++i)
head[i] = -1;
memset(edge, 0, sizeof(edge));
memset(money, 0, sizeof(money));
memset(indgr, 0, sizeof(indgr));
int a, b;
for (int i = 1; i <= m; ++i)
{
scanf("%d%d", &a, &b);
add_edge(b, a);//加边的同时
indgr[a]++;
}
if (topsort()) {
printf("-1\n");
continue;
}
long long ans = n * 888;
for (int i = 1; i <= n; ans += money[i], ++i);
cout << ans << endl;
}
return 0;
}
上面这个代码有很多错误
下面更正了一小时,结果还是wa
感觉职业生涯到头了
今日AC数:0
今日总结
没用
明天目标
困死
今日鸡汤
无