結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー 👑 kmjpkmjp
提出日時 2021-01-22 23:18:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,711 bytes
コンパイル時間 2,539 ms
コンパイル使用メモリ 218,072 KB
実行使用メモリ 143,888 KB
最終ジャッジ日時 2023-08-27 22:28:53
合計ジャッジ時間 12,846 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
114,168 KB
testcase_01 AC 39 ms
114,136 KB
testcase_02 AC 39 ms
114,064 KB
testcase_03 AC 38 ms
114,024 KB
testcase_04 AC 38 ms
114,328 KB
testcase_05 AC 39 ms
114,036 KB
testcase_06 AC 39 ms
114,032 KB
testcase_07 AC 39 ms
114,020 KB
testcase_08 AC 49 ms
115,916 KB
testcase_09 AC 43 ms
114,596 KB
testcase_10 AC 47 ms
115,488 KB
testcase_11 AC 47 ms
115,356 KB
testcase_12 AC 48 ms
115,336 KB
testcase_13 AC 237 ms
135,376 KB
testcase_14 AC 284 ms
136,644 KB
testcase_15 AC 324 ms
138,356 KB
testcase_16 AC 212 ms
131,320 KB
testcase_17 AC 125 ms
128,712 KB
testcase_18 AC 378 ms
143,824 KB
testcase_19 AC 383 ms
143,752 KB
testcase_20 AC 390 ms
143,844 KB
testcase_21 AC 390 ms
143,888 KB
testcase_22 AC 389 ms
143,768 KB
testcase_23 AC 93 ms
123,232 KB
testcase_24 AC 72 ms
118,524 KB
testcase_25 AC 180 ms
129,128 KB
testcase_26 AC 273 ms
134,944 KB
testcase_27 AC 174 ms
128,172 KB
testcase_28 AC 131 ms
123,644 KB
testcase_29 AC 159 ms
127,632 KB
testcase_30 AC 138 ms
123,780 KB
testcase_31 AC 108 ms
124,172 KB
testcase_32 AC 127 ms
125,880 KB
testcase_33 AC 240 ms
131,288 KB
testcase_34 AC 257 ms
133,948 KB
testcase_35 WA -
testcase_36 AC 279 ms
136,008 KB
testcase_37 AC 118 ms
124,740 KB
testcase_38 AC 150 ms
128,408 KB
testcase_39 AC 152 ms
128,504 KB
testcase_40 AC 150 ms
128,348 KB
testcase_41 AC 151 ms
128,428 KB
testcase_42 AC 153 ms
128,488 KB
testcase_43 AC 136 ms
141,372 KB
testcase_44 AC 103 ms
134,056 KB
testcase_45 AC 124 ms
137,784 KB
testcase_46 AC 51 ms
121,400 KB
testcase_47 AC 39 ms
114,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N,M;
vector<vector<int>> E[202020];
vector<int> RE[202020];
int reach[202020];
class SCC {
public:
	static const int MV = 2025000;
	vector<vector<int> > SC; int NV,GR[MV];
private:
	vector<int> E[MV], RE[MV], NUM; int vis[MV];
public:
	void init(int NV) { this->NV=NV; for(int i=0;i<NV;i++) { E[i].clear(); RE[i].clear();}}
	void add_edge(int x,int y) { E[x].push_back(y); RE[y].push_back(x); }
	void dfs(int cu) { vis[cu]=1; for(int i=0;i<E[cu].size();i++) if(!vis[E[cu][i]]) dfs(E[cu][i]); NUM.push_back(cu); }
	void revdfs(int cu, int ind) { int i; vis[cu]=1; GR[cu]=ind; SC[ind].push_back(cu);
		FOR(i,RE[cu].size()) if(!vis[RE[cu][i]]) revdfs(RE[cu][i],ind);}
	void scc() {
		int c=0,i; SC.clear(); SC.resize(NV); NUM.clear();
		assert(NV);
		FOR(i,NV) vis[i]=0; FOR(i,NV) if(!vis[i]) dfs(i); FOR(i,NV) vis[i]=0;
		for(int i=NUM.size()-1;i>=0;i--) if(!vis[NUM[i]]){
			SC[c].clear(); revdfs(NUM[i],c); sort(SC[c].begin(),SC[c].end()); c++;
		}
		SC.resize(c);
	}
};
SCC scc;

ll num[202020];
ll sum[202020];
int in[202020];
const ll mo=1000000007;
void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M;
	scc.init(N+1);
	FOR(i,M) {
		cin>>x>>y>>j>>l;
		E[x].push_back({y,j,l});
		RE[y].push_back(x);
		scc.add_edge(x,y);
	}
	queue<int> Q;
	reach[N]=1;
	Q.push(N);
	while(Q.size()) {
		x=Q.front();
		Q.pop();
		FORR(e,RE[x]) if(reach[e]==0) {
			reach[e]=1;
			Q.push(e);
		}
	}
	scc.scc();
	FOR(i,N) if(reach[i]&&scc.SC[scc.GR[i]].size()>1) {
		cout<<"INF"<<endl;
		return;
	}
	if(reach[0]==0) return _P("0\n");
	
	FOR(i,N+1) {
		FORR(e,E[i]) if(reach[i]&&reach[e[0]]) in[e[0]]++;
	}
	
	FOR(i,N+1) if(reach[i]&&in[i]==0) Q.push(i);
	num[0]=1;
	sum[0]=0;
	
	while(Q.size()) {
		x=Q.front();
		Q.pop();
		FORR(e,E[x]) if(reach[e[0]]) {
			(num[e[0]]+=num[x]*e[2])%=mo;
			(sum[e[0]]+=e[2]*(sum[x]+num[x]*e[1]%mo))%=mo;
			in[e[0]]--;
			if(in[e[0]]==0) Q.push(e[0]);
		}
	}
	
	cout<<sum[N]<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0