結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー 👑 kmjpkmjp
提出日時 2021-01-22 23:23:00
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 386 ms / 2,500 ms
コード長 2,712 bytes
コンパイル時間 2,598 ms
コンパイル使用メモリ 220,308 KB
実行使用メモリ 140,960 KB
最終ジャッジ日時 2023-08-28 12:09:04
合計ジャッジ時間 12,692 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
112,596 KB
testcase_01 AC 39 ms
112,592 KB
testcase_02 AC 38 ms
112,592 KB
testcase_03 AC 39 ms
112,292 KB
testcase_04 AC 38 ms
112,436 KB
testcase_05 AC 39 ms
112,340 KB
testcase_06 AC 39 ms
112,496 KB
testcase_07 AC 39 ms
112,556 KB
testcase_08 AC 49 ms
113,988 KB
testcase_09 AC 42 ms
112,856 KB
testcase_10 AC 49 ms
113,700 KB
testcase_11 AC 48 ms
113,724 KB
testcase_12 AC 49 ms
113,940 KB
testcase_13 AC 231 ms
132,108 KB
testcase_14 AC 286 ms
133,148 KB
testcase_15 AC 300 ms
135,064 KB
testcase_16 AC 197 ms
127,736 KB
testcase_17 AC 119 ms
125,408 KB
testcase_18 AC 373 ms
140,960 KB
testcase_19 AC 373 ms
140,712 KB
testcase_20 AC 378 ms
140,868 KB
testcase_21 AC 379 ms
140,928 KB
testcase_22 AC 386 ms
140,932 KB
testcase_23 AC 90 ms
119,684 KB
testcase_24 AC 72 ms
116,872 KB
testcase_25 AC 170 ms
125,512 KB
testcase_26 AC 260 ms
131,396 KB
testcase_27 AC 168 ms
126,568 KB
testcase_28 AC 132 ms
122,180 KB
testcase_29 AC 161 ms
126,000 KB
testcase_30 AC 134 ms
122,096 KB
testcase_31 AC 109 ms
120,872 KB
testcase_32 AC 126 ms
124,132 KB
testcase_33 AC 230 ms
129,680 KB
testcase_34 AC 251 ms
130,604 KB
testcase_35 AC 295 ms
134,424 KB
testcase_36 AC 275 ms
132,876 KB
testcase_37 AC 118 ms
123,212 KB
testcase_38 AC 150 ms
126,860 KB
testcase_39 AC 149 ms
127,000 KB
testcase_40 AC 153 ms
126,612 KB
testcase_41 AC 153 ms
126,816 KB
testcase_42 AC 150 ms
126,888 KB
testcase_43 AC 138 ms
138,304 KB
testcase_44 AC 103 ms
130,584 KB
testcase_45 AC 126 ms
134,780 KB
testcase_46 AC 51 ms
118,308 KB
testcase_47 AC 39 ms
112,280 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<ll>> 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();
	if(reach[0]==0) return _P("0\n");
	FOR(i,N) if(reach[i]&&scc.SC[scc.GR[i]].size()>1) {
		cout<<"INF"<<endl;
		return;
	}
	
	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